src/masternode/masternodeman.cpp
CMasternode* CMasternodeMan::GetNextMasternodeInQueueForPayment(int nBlockHeight, bool fFilterSigTime, int& nCount)
{
    LOCK(cs);

    CMasternode *pBestMasternode = NULL;
    std::vector<std::pair<int64_t, CTxIn> > vecMasternodeLastPaid;

    /*
        Make a vector with all of the last paid times
    */

    int nMnCount = CountEnabled();
    for (auto& mn : vMasternodes)
    {
        mn.Check();
        if(!mn.IsEnabled()) continue;

        // //check protocol version
        if(mn.protocolVersion < masternodePayments.GetMinMasternodePaymentsProto()) continue;

        //it's in the list (up to 8 entries ahead of current block to allow propagation) -- so let's skip it
        if(masternodePayments.IsScheduled(mn, nBlockHeight)) continue;

        // For security reasons and for network stability there is a delay to get the first reward.
        // The time is calculated as a product of 60 block and node count.
        if(fFilterSigTime && mn.sigTime + (nMnCount * 1 * 60) > GetAdjustedTime()) continue;

        //make sure it has as many confirmations as there are masternodes
        if(mn.GetMasternodeInputAge() < nMnCount) continue;

        vecMasternodeLastPaid.push_back(std::make_pair(mn.SecondsSincePayment(), mn.vin));
    }

    nCount = (int)vecMasternodeLastPaid.size();

    //when the network is in the process of upgrading, don't penalize nodes that recently restarted
    if(fFilterSigTime && nCount < nMnCount/3) return GetNextMasternodeInQueueForPayment(nBlockHeight, false, nCount);

    // Sort them high to low
    std::sort(vecMasternodeLastPaid.rbegin(), vecMasternodeLastPaid.rend(), CompareLastPaid());

    // Look at 1/10 of the oldest nodes (by last payment), calculate their scores and pay the best one
    //  -- This doesn't look at who is being paid in the +8-10 blocks, allowing for double payments very rarely
    //  -- 1/100 payments should be a double payment on mainnet - (1/(3000/10))*2
    //  -- (chance per block * chances before IsScheduled will fire)
    int nTenthNetwork = CountEnabled()/10;
    int nCountTenth = 0; 
    arith_uint256 nHigh = 0;
    for (auto& s : vecMasternodeLastPaid) {
        CMasternode* pmn = Find(s.second);
        if(!pmn) break;

        arith_uint256 n = pmn->CalculateScore(nBlockHeight - 100);
        if(n > nHigh){
            nHigh = n;
            pBestMasternode = pmn;
        }
        nCountTenth++;
        if(nCountTenth >= nTenthNetwork) break;
    }
    return pBestMasternode;
}
// 
// Deterministically select the oldest/best masternode to pay on the network
//
Deterministically select the oldest/best masternode to pay on the network
Deterministically select the oldest / best masternode to pay on the network
CMasternode* CMasternodeMan::GetNextMasternodeInQueueForPayment(int nBlockHeight, bool fFilterSigTime, int& nCount)
{
    LOCK(cs);
    CMasternode *pBestMasternode = NULL;
    std::vector<std::pair<int64_t, CTxIn> > vecMasternodeLastPaid;
    int nMnCount = CountEnabled();
    for (auto& mn : vMasternodes)
    {
        mn.Check();
        if(!mn.IsEnabled()) continue;
        if(mn.protocolVersion < masternodePayments.GetMinMasternodePaymentsProto()) continue;
        if(masternodePayments.IsScheduled(mn, nBlockHeight)) continue;
        if(fFilterSigTime && mn.sigTime + (nMnCount * 1 * 60) > GetAdjustedTime()) continue;
        if(mn.GetMasternodeInputAge() < nMnCount) continue;
        vecMasternodeLastPaid.push_back(std::make_pair(mn.SecondsSincePayment(), mn.vin));
    }
    nCount = (int)vecMasternodeLastPaid.size();
    if(fFilterSigTime && nCount < nMnCount/3) return GetNextMasternodeInQueueForPayment(nBlockHeight, false, nCount);
    std::sort(vecMasternodeLastPaid.rbegin(), vecMasternodeLastPaid.rend(), CompareLastPaid());
    int nTenthNetwork = CountEnabled()/10;
    int nCountTenth = 0; 
    arith_uint256 nHigh = 0;
    for (auto& s : vecMasternodeLastPaid) {
        CMasternode* pmn = Find(s.second);
        if(!pmn) break;
        arith_uint256 n = pmn->CalculateScore(nBlockHeight - 100);
        if(n > nHigh){
            nHigh = n;
            pBestMasternode = pmn;
        }
        nCountTenth++;
        if(nCountTenth >= nTenthNetwork) break;
    }
    return pBestMasternode;
}
CMasternode * CMasternodeMan :: GetNextMasternodeInQueueForPayment ( int nBlockHeight , bool fFilterSigTime , int & nCount ) { LOCK ( cs ) ; CMasternode * pBestMasternode = NULL ; std :: vector < std :: pair < int64_t , CTxIn > > vecMasternodeLastPaid ; int nMnCount = CountEnabled ( ) ; for ( auto & mn : vMasternodes ) { mn . Check ( ) ; if ( ! mn . IsEnabled ( ) ) continue ; if ( mn . protocolVersion < masternodePayments . GetMinMasternodePaymentsProto ( ) ) continue ; if ( masternodePayments . IsScheduled ( mn , nBlockHeight ) ) continue ; if ( fFilterSigTime && mn . sigTime + ( nMnCount * 1 * 60 ) > GetAdjustedTime ( ) ) continue ; if ( mn . GetMasternodeInputAge ( ) < nMnCount ) continue ; vecMasternodeLastPaid . push_back ( std :: make_pair ( mn . SecondsSincePayment ( ) , mn . vin ) ) ; } nCount = ( int ) vecMasternodeLastPaid . size ( ) ; if ( fFilterSigTime && nCount < nMnCount / 3 ) return GetNextMasternodeInQueueForPayment ( nBlockHeight , false , nCount ) ; std :: sort ( vecMasternodeLastPaid . rbegin ( ) , vecMasternodeLastPaid . rend ( ) , CompareLastPaid ( ) ) ; int nTenthNetwork = CountEnabled ( ) / 10 ; int nCountTenth = 0 ; arith_uint256 nHigh = 0 ; for ( auto & s : vecMasternodeLastPaid ) { CMasternode * pmn = Find ( s . second ) ; if ( ! pmn ) break ; arith_uint256 n = pmn -> CalculateScore ( nBlockHeight - 100 ) ; if ( n > nHigh ) { nHigh = n ; pBestMasternode = pmn ; } nCountTenth ++ ; if ( nCountTenth >= nTenthNetwork ) break ; } return pBestMasternode ; }
Deterministically select the oldest/best masternode to pay on the network
Deterministically select the oldest / best masternode to pay on the network
/*
        Make a vector with all of the last paid times
    */ // //check protocol version //it's in the list (up to 8 entries ahead of current block to allow propagation) -- so let's skip it // For security reasons and for network stability there is a delay to get the first reward. // The time is calculated as a product of 60 block and node count. //make sure it has as many confirmations as there are masternodes //when the network is in the process of upgrading, don't penalize nodes that recently restarted // Sort them high to low // Look at 1/10 of the oldest nodes (by last payment), calculate their scores and pay the best one //  -- This doesn't look at who is being paid in the +8-10 blocks, allowing for double payments very rarely //  -- 1/100 payments should be a double payment on mainnet - (1/(3000/10))*2 //  -- (chance per block * chances before IsScheduled will fire)