@@ -749,48 +749,68 @@ bool CMasternodePayments::ProcessBlock(int nBlockHeight)
749749{
750750 LOCK (cs_masternodes);
751751 if (!enabled) return false ;
752- CMasternodePaymentWinner winner ;
752+ CMasternodePaymentWinner newWinner ;
753753
754754 std::vector<CTxIn> vecLastPayments;
755- int c = 0 ;
756- BOOST_REVERSE_FOREACH (CMasternodePaymentWinner& winner, vWinning){
755+ BOOST_REVERSE_FOREACH (CMasternodePaymentWinner& winner, vWinning)
756+ {
757+ // if we already have the same vin - we have one full payment cycle, break
758+ if (vecLastPayments.size () > 0
759+ && std::find (vecLastPayments.begin (), vecLastPayments.end (), winner.vin ) != vecLastPayments.end ()) break ;
757760 vecLastPayments.push_back (winner.vin );
758- // if we have one full payment cycle, break
759- if (++c > (int )vecMasternodes.size ()) break ;
760761 }
761762
762763 std::random_shuffle ( vecMasternodes.begin (), vecMasternodes.end () );
763- BOOST_FOREACH (CMasterNode& mn, vecMasternodes) {
764+ BOOST_FOREACH (CMasterNode& mn, vecMasternodes)
765+ {
764766 bool found = false ;
765767 BOOST_FOREACH (CTxIn& vin, vecLastPayments)
766- if (mn.vin == vin) found = true ;
768+ if (mn.vin == vin)
769+ {
770+ found = true ;
771+ break ;
772+ }
767773
768774 if (found) continue ;
769775
770776 mn.Check ();
771- if (!mn.IsEnabled ()) {
772- continue ;
773- }
777+ if (!mn.IsEnabled ()) continue ;
774778
775- winner .score = 0 ;
776- winner .nBlockHeight = nBlockHeight;
777- winner .vin = mn.vin ;
778- winner .payee = GetScriptForDestination (mn.pubkey .GetID ());
779+ newWinner .score = 0 ;
780+ newWinner .nBlockHeight = nBlockHeight;
781+ newWinner .vin = mn.vin ;
782+ newWinner .payee . SetDestination (mn.pubkey .GetID ());
779783
780784 break ;
781785 }
782786
783- // if we can't find someone to get paid, pick randomly
784- if (winner.nBlockHeight == 0 && vecMasternodes.size () > 0 ) {
785- winner.score = 0 ;
786- winner.nBlockHeight = nBlockHeight;
787- winner.vin = vecMasternodes[0 ].vin ;
788- winner.payee =GetScriptForDestination (vecMasternodes[0 ].pubkey .GetID ());
787+ // if we can't find new MN to get paid, pick first active MN counting back from the end of vecLastPayments list
788+ if (newWinner.nBlockHeight == 0 && vecMasternodes.size () > 1 )
789+ {
790+ BOOST_REVERSE_FOREACH (CTxIn& vin, vecLastPayments)
791+ {
792+ BOOST_FOREACH (CMasterNode& mn, vecMasternodes)
793+ if (mn.vin == vin)
794+ {
795+ mn.Check ();
796+ if (!mn.IsEnabled ()) break ;
797+
798+ newWinner.score = 0 ;
799+ newWinner.nBlockHeight = nBlockHeight;
800+ newWinner.vin = vin;
801+ newWinner.payee .SetDestination (mn.pubkey .GetID ());
802+ break ;
803+ }
804+
805+ if (newWinner.nBlockHeight != 0 ) break ; // we found active MN
806+ }
789807 }
790808
791- if (Sign (winner)){
792- if (AddWinningMasternode (winner)){
793- Relay (winner);
809+ if (Sign (newWinner))
810+ {
811+ if (AddWinningMasternode (newWinner))
812+ {
813+ Relay (newWinner);
794814 return true ;
795815 }
796816 }
0 commit comments