Change GetMasternodeScores and GetMasternodeRank/s to use CDeterministicMNCPtr

This leaves us with nMinProtocol unused, but this is ok as we will later
remove that argument completely.
This commit is contained in:
Alexander Block 2018-12-17 16:09:07 +01:00
parent 17c792cd3b
commit 37541ee00e
4 changed files with 29 additions and 44 deletions

View File

@ -245,8 +245,7 @@ void CInstantSend::Vote(CTxLockCandidate& txLockCandidate, CConnman& connman)
int nRank; int nRank;
uint256 quorumModifierHash; uint256 quorumModifierHash;
int nMinRequiredProtocol = std::max(MIN_INSTANTSEND_PROTO_VERSION, mnpayments.GetMinMasternodePaymentsProto()); if (!mnodeman.GetMasternodeRank(activeMasternodeInfo.outpoint, nRank, quorumModifierHash, nLockInputHeight)) {
if (!mnodeman.GetMasternodeRank(activeMasternodeInfo.outpoint, nRank, quorumModifierHash, nLockInputHeight, nMinRequiredProtocol)) {
LogPrint("instantsend", "CInstantSend::Vote -- Can't calculate rank for masternode %s\n", activeMasternodeInfo.outpoint.ToStringShort()); LogPrint("instantsend", "CInstantSend::Vote -- Can't calculate rank for masternode %s\n", activeMasternodeInfo.outpoint.ToStringShort());
continue; continue;
} }
@ -1061,8 +1060,7 @@ bool CTxLockVote::IsValid(CNode* pnode, CConnman& connman) const
int nRank; int nRank;
uint256 expectedQuorumModifierHash; uint256 expectedQuorumModifierHash;
int nMinRequiredProtocol = std::max(MIN_INSTANTSEND_PROTO_VERSION, mnpayments.GetMinMasternodePaymentsProto()); if (!mnodeman.GetMasternodeRank(outpointMasternode, nRank, expectedQuorumModifierHash, nLockInputHeight)) {
if (!mnodeman.GetMasternodeRank(outpointMasternode, nRank, expectedQuorumModifierHash, nLockInputHeight, nMinRequiredProtocol)) {
//can be caused by past versions trying to vote with an invalid protocol //can be caused by past versions trying to vote with an invalid protocol
LogPrint("instantsend", "CTxLockVote::IsValid -- Can't calculate rank for masternode %s\n", outpointMasternode.ToStringShort()); LogPrint("instantsend", "CTxLockVote::IsValid -- Can't calculate rank for masternode %s\n", outpointMasternode.ToStringShort());
return false; return false;

View File

@ -1046,7 +1046,7 @@ void CMasternodePayments::CheckBlockVotes(int nBlockHeight)
voteHash.ToString()); voteHash.ToString());
continue; continue;
} }
if (itVote->second.masternodeOutpoint == mn.second.outpoint) { if (itVote->second.masternodeOutpoint == mn.second->collateralOutpoint) {
payee = itVote->second.payee; payee = itVote->second.payee;
found = true; found = true;
break; break;
@ -1061,12 +1061,12 @@ void CMasternodePayments::CheckBlockVotes(int nBlockHeight)
CBitcoinAddress address2(address1); CBitcoinAddress address2(address1);
debugStr += strprintf(" - %s - voted for %s\n", debugStr += strprintf(" - %s - voted for %s\n",
mn.second.outpoint.ToStringShort(), address2.ToString()); mn.second->collateralOutpoint.ToStringShort(), address2.ToString());
} else { } else {
mapMasternodesDidNotVote.emplace(mn.second.outpoint, 0).first->second++; mapMasternodesDidNotVote.emplace(mn.second->collateralOutpoint, 0).first->second++;
debugStr += strprintf(" - %s - no vote received\n", debugStr += strprintf(" - %s - no vote received\n",
mn.second.outpoint.ToStringShort()); mn.second->collateralOutpoint.ToStringShort());
} }
if (++i >= MNPAYMENTS_SIGNATURES_TOTAL) break; if (++i >= MNPAYMENTS_SIGNATURES_TOTAL) break;

View File

@ -42,10 +42,10 @@ struct CompareLastPaidBlock
struct CompareScoreMN struct CompareScoreMN
{ {
bool operator()(const std::pair<arith_uint256, const CMasternode*>& t1, bool operator()(const std::pair<arith_uint256, const CDeterministicMNCPtr&>& t1,
const std::pair<arith_uint256, const CMasternode*>& t2) const const std::pair<arith_uint256, const CDeterministicMNCPtr&>& t2) const
{ {
return (t1.first != t2.first) ? (t1.first < t2.first) : (t1.second->outpoint < t2.second->outpoint); return (t1.first != t2.first) ? (t1.first < t2.first) : (t1.second->collateralOutpoint < t2.second->collateralOutpoint);
} }
}; };
@ -257,9 +257,9 @@ void CMasternodeMan::CheckAndRemove(CConnman& connman)
// ask first MNB_RECOVERY_QUORUM_TOTAL masternodes we can connect to and we haven't asked recently // ask first MNB_RECOVERY_QUORUM_TOTAL masternodes we can connect to and we haven't asked recently
for(int i = 0; setRequested.size() < MNB_RECOVERY_QUORUM_TOTAL && i < (int)vecMasternodeRanks.size(); i++) { for(int i = 0; setRequested.size() < MNB_RECOVERY_QUORUM_TOTAL && i < (int)vecMasternodeRanks.size(); i++) {
// avoid banning // avoid banning
if(mWeAskedForMasternodeListEntry.count(it->first) && mWeAskedForMasternodeListEntry[it->first].count(vecMasternodeRanks[i].second.addr)) continue; if(mWeAskedForMasternodeListEntry.count(it->first) && mWeAskedForMasternodeListEntry[it->first].count(vecMasternodeRanks[i].second->pdmnState->addr)) continue;
// didn't ask recently, ok to ask now // didn't ask recently, ok to ask now
CService addr = vecMasternodeRanks[i].second.addr; CService addr = vecMasternodeRanks[i].second->pdmnState->addr;
setRequested.insert(addr); setRequested.insert(addr);
listScheduledMnbRequestConnections.push_back(std::make_pair(addr, hash)); listScheduledMnbRequestConnections.push_back(std::make_pair(addr, hash));
fAskedForMnbRecovery = true; fAskedForMnbRecovery = true;
@ -766,33 +766,18 @@ std::map<COutPoint, CMasternode> CMasternodeMan::GetFullMasternodeMap()
} }
} }
bool CMasternodeMan::GetMasternodeScores(const uint256& nBlockHash, CMasternodeMan::score_pair_vec_t& vecMasternodeScoresRet, int nMinProtocol) bool CMasternodeMan::GetMasternodeScores(const uint256& nBlockHash, CMasternodeMan::score_pair_vec_t& vecMasternodeScoresRet)
{ {
AssertLockHeld(cs); AssertLockHeld(cs);
vecMasternodeScoresRet.clear(); vecMasternodeScoresRet.clear();
if (deterministicMNManager->IsDIP3Active()) { auto mnList = deterministicMNManager->GetListAtChainTip();
auto mnList = deterministicMNManager->GetListAtChainTip(); auto scores = mnList.CalculateScores(nBlockHash);
auto scores = mnList.CalculateScores(nBlockHash); for (const auto& p : scores) {
for (const auto& p : scores) { vecMasternodeScoresRet.emplace_back(p.first, p.second);
auto* mn = Find(p.second->collateralOutpoint);
vecMasternodeScoresRet.emplace_back(p.first, mn);
}
} else {
if (!masternodeSync.IsMasternodeListSynced())
return false;
if (mapMasternodes.empty())
return false;
// calculate scores
for (const auto& mnpair : mapMasternodes) {
if (mnpair.second.nProtocolVersion >= nMinProtocol) {
vecMasternodeScoresRet.push_back(std::make_pair(mnpair.second.CalculateScore(nBlockHash), &mnpair.second));
}
}
} }
sort(vecMasternodeScoresRet.rbegin(), vecMasternodeScoresRet.rend(), CompareScoreMN()); sort(vecMasternodeScoresRet.rbegin(), vecMasternodeScoresRet.rend(), CompareScoreMN());
return !vecMasternodeScoresRet.empty(); return !vecMasternodeScoresRet.empty();
} }
@ -820,13 +805,13 @@ bool CMasternodeMan::GetMasternodeRank(const COutPoint& outpoint, int& nRankRet,
LOCK(cs); LOCK(cs);
score_pair_vec_t vecMasternodeScores; score_pair_vec_t vecMasternodeScores;
if (!GetMasternodeScores(blockHashRet, vecMasternodeScores, nMinProtocol)) if (!GetMasternodeScores(blockHashRet, vecMasternodeScores))
return false; return false;
int nRank = 0; int nRank = 0;
for (const auto& scorePair : vecMasternodeScores) { for (const auto& scorePair : vecMasternodeScores) {
nRank++; nRank++;
if(scorePair.second->outpoint == outpoint) { if(scorePair.second->collateralOutpoint == outpoint) {
nRankRet = nRank; nRankRet = nRank;
return true; return true;
} }
@ -852,13 +837,13 @@ bool CMasternodeMan::GetMasternodeRanks(CMasternodeMan::rank_pair_vec_t& vecMast
LOCK(cs); LOCK(cs);
score_pair_vec_t vecMasternodeScores; score_pair_vec_t vecMasternodeScores;
if (!GetMasternodeScores(nBlockHash, vecMasternodeScores, nMinProtocol)) if (!GetMasternodeScores(nBlockHash, vecMasternodeScores))
return false; return false;
int nRank = 0; int nRank = 0;
for (const auto& scorePair : vecMasternodeScores) { for (const auto& scorePair : vecMasternodeScores) {
nRank++; nRank++;
vecMasternodeRanksRet.push_back(std::make_pair(nRank, *scorePair.second)); vecMasternodeRanksRet.push_back(std::make_pair(nRank, scorePair.second));
} }
return true; return true;
@ -1188,7 +1173,7 @@ void CMasternodeMan::DoFullVerificationStep(CConnman& connman)
(int)MAX_POSE_RANK); (int)MAX_POSE_RANK);
return; return;
} }
if(rankPair.second.outpoint == activeMasternodeInfo.outpoint) { if(rankPair.second->collateralOutpoint == activeMasternodeInfo.outpoint) {
nMyRank = rankPair.first; nMyRank = rankPair.first;
LogPrint("masternode", "CMasternodeMan::DoFullVerificationStep -- Found self at rank %d/%d, verifying up to %d masternodes\n", LogPrint("masternode", "CMasternodeMan::DoFullVerificationStep -- Found self at rank %d/%d, verifying up to %d masternodes\n",
nMyRank, nRanksTotal, (int)MAX_POSE_CONNECTIONS); nMyRank, nRanksTotal, (int)MAX_POSE_CONNECTIONS);
@ -1218,8 +1203,8 @@ void CMasternodeMan::DoFullVerificationStep(CConnman& connman)
continue; continue;
} }
LogPrint("masternode", "CMasternodeMan::DoFullVerificationStep -- Verifying masternode %s rank %d/%d address %s\n", LogPrint("masternode", "CMasternodeMan::DoFullVerificationStep -- Verifying masternode %s rank %d/%d address %s\n",
it->second.outpoint.ToStringShort(), it->first, nRanksTotal, it->second.addr.ToString()); it->second->collateralOutpoint.ToStringShort(), it->first, nRanksTotal, it->second->pdmnState->addr.ToString());
CAddress addr = CAddress(it->second.addr, NODE_NETWORK); CAddress addr = CAddress(it->second->pdmnState->addr, NODE_NETWORK);
if(CheckVerifyRequestAddr(addr, connman)) { if(CheckVerifyRequestAddr(addr, connman)) {
vAddr.push_back(addr); vAddr.push_back(addr);
if((int)vAddr.size() >= MAX_POSE_CONNECTIONS) break; if((int)vAddr.size() >= MAX_POSE_CONNECTIONS) break;

View File

@ -8,6 +8,8 @@
#include "masternode.h" #include "masternode.h"
#include "sync.h" #include "sync.h"
#include "evo/deterministicmns.h"
class CMasternodeMan; class CMasternodeMan;
class CConnman; class CConnman;
@ -16,9 +18,9 @@ extern CMasternodeMan mnodeman;
class CMasternodeMan class CMasternodeMan
{ {
public: public:
typedef std::pair<arith_uint256, const CMasternode*> score_pair_t; typedef std::pair<arith_uint256, CDeterministicMNCPtr> score_pair_t;
typedef std::vector<score_pair_t> score_pair_vec_t; typedef std::vector<score_pair_t> score_pair_vec_t;
typedef std::pair<int, const CMasternode> rank_pair_t; typedef std::pair<int, CDeterministicMNCPtr> rank_pair_t;
typedef std::vector<rank_pair_t> rank_pair_vec_t; typedef std::vector<rank_pair_t> rank_pair_vec_t;
private: private:
@ -80,7 +82,7 @@ private:
/// Find an entry /// Find an entry
CMasternode* Find(const COutPoint& outpoint); CMasternode* Find(const COutPoint& outpoint);
bool GetMasternodeScores(const uint256& nBlockHash, score_pair_vec_t& vecMasternodeScoresRet, int nMinProtocol = 0); bool GetMasternodeScores(const uint256& nBlockHash, score_pair_vec_t& vecMasternodeScoresRet);
void SyncSingle(CNode* pnode, const COutPoint& outpoint, CConnman& connman); void SyncSingle(CNode* pnode, const COutPoint& outpoint, CConnman& connman);
void SyncAll(CNode* pnode, CConnman& connman); void SyncAll(CNode* pnode, CConnman& connman);