diff --git a/src/masternode.cpp b/src/masternode.cpp index a86a658e6..c7790925e 100644 --- a/src/masternode.cpp +++ b/src/masternode.cpp @@ -735,23 +735,56 @@ void CMasternodePayments::CleanPaymentList() bool CMasternodePayments::ProcessBlock(int nBlockHeight) { - if(!enabled) return false; CMasternodePaymentWinner winner; + LogPrintf("CMasternodePayments() : block %d \n", nBlockHeight); + //only do this once per blockheight + CScript payee; + if(GetBlockPayee(nBlockHeight, payee)) return true; + + //randomly sort our masternodes + std::random_shuffle ( vecMasternodes.begin(), vecMasternodes.end() ); + + //count the active masternodes + int nActiveMasternodes = 0; + BOOST_FOREACH(CMasterNode& mn, vecMasternodes) { + mn.Check(); + if(!mn.IsEnabled()) { + continue; + } + + nActiveMasternodes++; + } + nActiveMasternodes *= 0.95; + + //make an array with 1 payment cycle of active masternodes std::vector vecLastPayments; int c = 0; BOOST_REVERSE_FOREACH(CMasternodePaymentWinner& winner, vWinning){ vecLastPayments.push_back(winner.vin); //if we have one full payment cycle, break - if(++c > (int)vecMasternodes.size()) break; + if(++c >= nActiveMasternodes) break; } - std::random_shuffle ( vecMasternodes.begin(), vecMasternodes.end() ); + //scan for the next winner BOOST_FOREACH(CMasterNode& mn, vecMasternodes) { bool found = false; - BOOST_FOREACH(CTxIn& vin, vecLastPayments) - if(mn.vin == vin) found = true; + BOOST_FOREACH(CTxIn& vin, vecLastPayments){ + if(mn.vin == vin) { + found = true; + break; + } + } + + CScript payee; + payee.SetDestination(mn.pubkey.GetID()); + + CTxDestination address1; + ExtractDestination(payee, address1); + CBitcoinAddress address2(address1); + + if(fDebug) LogPrintf("CMasternodePayments() : Looking for next payee - %s %d \n", address2.ToString().c_str(), found); if(found) continue; @@ -760,20 +793,31 @@ bool CMasternodePayments::ProcessBlock(int nBlockHeight) continue; } + winner.score = 0; winner.nBlockHeight = nBlockHeight; winner.vin = mn.vin; winner.payee.SetDestination(mn.pubkey.GetID()); + if(fDebug) LogPrintf("CMasternodePayments() : got payee - %s \n", address2.ToString().c_str()); + break; } - //if we can't find someone to get paid, pick randomly + //if we can't find someone to get paid, pick randomly (shouldn't happen) if(winner.nBlockHeight == 0 && vecMasternodes.size() > 1) { + int i = rand() % (int)(vecMasternodes.size() - 1); winner.score = 0; winner.nBlockHeight = nBlockHeight; - winner.vin = vecMasternodes[0].vin; - winner.payee.SetDestination(vecMasternodes[0].pubkey.GetID()); + winner.vin = vecMasternodes[i].vin; + winner.payee.SetDestination(vecMasternodes[i].pubkey.GetID()); + + CTxDestination address1; + ExtractDestination(winner.payee, address1); + CBitcoinAddress address2(address1); + + if(fDebug) LogPrintf("CMasternodePayments() : Looking for next payee - %s \n", address2.ToString().c_str()); + } if(Sign(winner)){