@@ -735,23 +735,56 @@ void CMasternodePayments::CleanPaymentList()
735735
736736bool CMasternodePayments::ProcessBlock (int nBlockHeight)
737737{
738-
739738 if (!enabled) return false ;
740739 CMasternodePaymentWinner winner;
740+ LogPrintf (" CMasternodePayments() : block %d \n " , nBlockHeight);
741+
742+ // only do this once per blockheight
743+ CScript payee;
744+ if (GetBlockPayee (nBlockHeight, payee)) return true ;
745+
746+ // randomly sort our masternodes
747+ std::random_shuffle ( vecMasternodes.begin (), vecMasternodes.end () );
748+
749+ // count the active masternodes
750+ int nActiveMasternodes = 0 ;
751+ BOOST_FOREACH (CMasterNode& mn, vecMasternodes) {
752+ mn.Check ();
753+ if (!mn.IsEnabled ()) {
754+ continue ;
755+ }
756+
757+ nActiveMasternodes++;
758+ }
759+ nActiveMasternodes *= 0.95 ;
741760
761+ // make an array with 1 payment cycle of active masternodes
742762 std::vector<CTxIn> vecLastPayments;
743763 int c = 0 ;
744764 BOOST_REVERSE_FOREACH (CMasternodePaymentWinner& winner, vWinning){
745765 vecLastPayments.push_back (winner.vin );
746766 // if we have one full payment cycle, break
747- if (++c > ( int )vecMasternodes. size () ) break ;
767+ if (++c >= nActiveMasternodes ) break ;
748768 }
749769
750- std::random_shuffle ( vecMasternodes. begin (), vecMasternodes. end () );
770+ // scan for the next winner
751771 BOOST_FOREACH (CMasterNode& mn, vecMasternodes) {
752772 bool found = false ;
753- BOOST_FOREACH (CTxIn& vin, vecLastPayments)
754- if (mn.vin == vin) found = true ;
773+ BOOST_FOREACH (CTxIn& vin, vecLastPayments){
774+ if (mn.vin == vin) {
775+ found = true ;
776+ break ;
777+ }
778+ }
779+
780+ CScript payee;
781+ payee.SetDestination (mn.pubkey .GetID ());
782+
783+ CTxDestination address1;
784+ ExtractDestination (payee, address1);
785+ CBitcoinAddress address2 (address1);
786+
787+ if (fDebug ) LogPrintf (" CMasternodePayments() : Looking for next payee - %s %d \n " , address2.ToString ().c_str (), found);
755788
756789 if (found) continue ;
757790
@@ -760,20 +793,31 @@ bool CMasternodePayments::ProcessBlock(int nBlockHeight)
760793 continue ;
761794 }
762795
796+
763797 winner.score = 0 ;
764798 winner.nBlockHeight = nBlockHeight;
765799 winner.vin = mn.vin ;
766800 winner.payee .SetDestination (mn.pubkey .GetID ());
767801
802+ if (fDebug ) LogPrintf (" CMasternodePayments() : got payee - %s \n " , address2.ToString ().c_str ());
803+
768804 break ;
769805 }
770806
771- // if we can't find someone to get paid, pick randomly
807+ // if we can't find someone to get paid, pick randomly (shouldn't happen)
772808 if (winner.nBlockHeight == 0 && vecMasternodes.size () > 1 ) {
809+ int i = rand () % (int )(vecMasternodes.size () - 1 );
773810 winner.score = 0 ;
774811 winner.nBlockHeight = nBlockHeight;
775- winner.vin = vecMasternodes[0 ].vin ;
776- winner.payee .SetDestination (vecMasternodes[0 ].pubkey .GetID ());
812+ winner.vin = vecMasternodes[i].vin ;
813+ winner.payee .SetDestination (vecMasternodes[i].pubkey .GetID ());
814+
815+ CTxDestination address1;
816+ ExtractDestination (winner.payee , address1);
817+ CBitcoinAddress address2 (address1);
818+
819+ if (fDebug ) LogPrintf (" CMasternodePayments() : Looking for next payee - %s \n " , address2.ToString ().c_str ());
820+
777821 }
778822
779823 if (Sign (winner)){
0 commit comments