From d5dcf2b668dfc7677ef9f83a5c52bf2fc7ea910f Mon Sep 17 00:00:00 2001 From: UdjinM6 Date: Thu, 13 Oct 2016 13:45:18 +0400 Subject: [PATCH] Refactor CalculateScore, move GetBlockHash out of it (#1068) --- src/masternode.cpp | 16 ++++------------ src/masternode.h | 2 +- src/masternodeman.cpp | 42 +++++++++++++++++++++++++----------------- 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/masternode.cpp b/src/masternode.cpp index a85039f0e..da2cebe82 100644 --- a/src/masternode.cpp +++ b/src/masternode.cpp @@ -119,28 +119,20 @@ bool CMasternode::UpdateFromNewBroadcast(CMasternodeBroadcast& mnb) // the proof of work for that block. The further away they are the better, the furthest will win the election // and get paid this block // -uint256 CMasternode::CalculateScore(int nBlockHeight) +arith_uint256 CMasternode::CalculateScore(const uint256& blockHash) { - uint256 hash; uint256 aux = ArithToUint256(UintToArith256(vin.prevout.hash) + vin.prevout.n); - if(!GetBlockHash(hash, nBlockHeight)) { - LogPrintf("CMasternode::CalculateScore -- ERROR: GetBlockHash() failed at nBlockHeight %d\n", nBlockHeight); - return uint256(); - } - CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION); - ss << hash; + ss << blockHash; arith_uint256 hash2 = UintToArith256(ss.GetHash()); CHashWriter ss2(SER_GETHASH, PROTOCOL_VERSION); - ss2 << hash; + ss2 << blockHash; ss2 << aux; arith_uint256 hash3 = UintToArith256(ss2.GetHash()); - arith_uint256 r = (hash3 > hash2 ? hash3 - hash2 : hash2 - hash3); - - return ArithToUint256(r); + return (hash3 > hash2 ? hash3 - hash2 : hash2 - hash3); } void CMasternode::Check(bool fForce) diff --git a/src/masternode.h b/src/masternode.h index 683585f13..acbc9d987 100644 --- a/src/masternode.h +++ b/src/masternode.h @@ -191,7 +191,7 @@ public: } // CALCULATE A RANK AGAINST OF GIVEN BLOCK - uint256 CalculateScore(int nBlockHeight = -1); + arith_uint256 CalculateScore(const uint256& blockHash); bool UpdateFromNewBroadcast(CMasternodeBroadcast& mnb); diff --git a/src/masternodeman.cpp b/src/masternodeman.cpp index 2d48ce8c7..696603641 100644 --- a/src/masternodeman.cpp +++ b/src/masternodeman.cpp @@ -335,17 +335,22 @@ CMasternode* CMasternodeMan::GetNextMasternodeInQueueForPayment(int nBlockHeight // Sort them low to high sort(vecMasternodeLastPaid.begin(), vecMasternodeLastPaid.end(), CompareLastPaidBlock()); + uint256 blockHash; + if(!GetBlockHash(blockHash, nBlockHeight - 101)) { + LogPrintf("CMasternode::GetNextMasternodeInQueueForPayment -- ERROR: GetBlockHash() failed at nBlockHeight %d\n", nBlockHeight - 101); + return NULL; + } // 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; + arith_uint256 nHighest = 0; BOOST_FOREACH (PAIRTYPE(int, CMasternode*)& s, vecMasternodeLastPaid){ - arith_uint256 n = UintToArith256(s.second->CalculateScore(nBlockHeight - 101)); - if(n > nHigh){ - nHigh = n; + arith_uint256 nScore = s.second->CalculateScore(blockHash); + if(nScore > nHighest){ + nHighest = nScore; pBestMasternode = s.second; } nCountTenth++; @@ -401,8 +406,8 @@ int CMasternodeMan::GetMasternodeRank(const CTxIn& vin, int64_t nBlockHeight, in std::vector > vecMasternodeScores; //make sure we know about this block - uint256 hash = uint256(); - if(!GetBlockHash(hash, nBlockHeight)) return -1; + uint256 blockHash = uint256(); + if(!GetBlockHash(blockHash, nBlockHeight)) return -1; LOCK(cs); @@ -413,10 +418,9 @@ int CMasternodeMan::GetMasternodeRank(const CTxIn& vin, int64_t nBlockHeight, in mn.Check(); if(!mn.IsEnabled()) continue; } - uint256 n = mn.CalculateScore(nBlockHeight); - int64_t n2 = UintToArith256(n).GetCompact(false); + int64_t nScore = mn.CalculateScore(blockHash).GetCompact(false); - vecMasternodeScores.push_back(std::make_pair(n2, &mn)); + vecMasternodeScores.push_back(std::make_pair(nScore, &mn)); } sort(vecMasternodeScores.rbegin(), vecMasternodeScores.rend(), CompareScoreMN()); @@ -436,8 +440,8 @@ std::vector > CMasternodeMan::GetMasternodeRanks(int64_t std::vector > vecMasternodeRanks; //make sure we know about this block - uint256 hash = uint256(); - if(!GetBlockHash(hash, nBlockHeight)) return vecMasternodeRanks; + uint256 blockHash = uint256(); + if(!GetBlockHash(blockHash, nBlockHeight)) return vecMasternodeRanks; LOCK(cs); @@ -448,10 +452,9 @@ std::vector > CMasternodeMan::GetMasternodeRanks(int64_t if(mn.nProtocolVersion < minProtocol || !mn.IsEnabled()) continue; - uint256 n = mn.CalculateScore(nBlockHeight); - int64_t n2 = UintToArith256(n).GetCompact(false); + int64_t nScore = mn.CalculateScore(blockHash).GetCompact(false); - vecMasternodeScores.push_back(std::make_pair(n2, &mn)); + vecMasternodeScores.push_back(std::make_pair(nScore, &mn)); } sort(vecMasternodeScores.rbegin(), vecMasternodeScores.rend(), CompareScoreMN()); @@ -471,6 +474,12 @@ CMasternode* CMasternodeMan::GetMasternodeByRank(int nRank, int64_t nBlockHeight LOCK(cs); + uint256 blockHash; + if(!GetBlockHash(blockHash, nBlockHeight)) { + LogPrintf("CMasternode::GetMasternodeByRank -- ERROR: GetBlockHash() failed at nBlockHeight %d\n", nBlockHeight); + return NULL; + } + // Fill scores BOOST_FOREACH(CMasternode& mn, vMasternodes) { @@ -480,10 +489,9 @@ CMasternode* CMasternodeMan::GetMasternodeByRank(int nRank, int64_t nBlockHeight if(!mn.IsEnabled()) continue; } - uint256 n = mn.CalculateScore(nBlockHeight); - int64_t n2 = UintToArith256(n).GetCompact(false); + int64_t nScore = mn.CalculateScore(blockHash).GetCompact(false); - vecMasternodeScores.push_back(std::make_pair(n2, &mn)); + vecMasternodeScores.push_back(std::make_pair(nScore, &mn)); } sort(vecMasternodeScores.rbegin(), vecMasternodeScores.rend(), CompareScoreMN());