Refactor CalculateScore, move GetBlockHash out of it (#1068)
This commit is contained in:
parent
8aaa8a22b0
commit
d5dcf2b668
@ -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
|
// 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
|
// 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);
|
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);
|
CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION);
|
||||||
ss << hash;
|
ss << blockHash;
|
||||||
arith_uint256 hash2 = UintToArith256(ss.GetHash());
|
arith_uint256 hash2 = UintToArith256(ss.GetHash());
|
||||||
|
|
||||||
CHashWriter ss2(SER_GETHASH, PROTOCOL_VERSION);
|
CHashWriter ss2(SER_GETHASH, PROTOCOL_VERSION);
|
||||||
ss2 << hash;
|
ss2 << blockHash;
|
||||||
ss2 << aux;
|
ss2 << aux;
|
||||||
arith_uint256 hash3 = UintToArith256(ss2.GetHash());
|
arith_uint256 hash3 = UintToArith256(ss2.GetHash());
|
||||||
|
|
||||||
arith_uint256 r = (hash3 > hash2 ? hash3 - hash2 : hash2 - hash3);
|
return (hash3 > hash2 ? hash3 - hash2 : hash2 - hash3);
|
||||||
|
|
||||||
return ArithToUint256(r);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CMasternode::Check(bool fForce)
|
void CMasternode::Check(bool fForce)
|
||||||
|
@ -191,7 +191,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CALCULATE A RANK AGAINST OF GIVEN BLOCK
|
// CALCULATE A RANK AGAINST OF GIVEN BLOCK
|
||||||
uint256 CalculateScore(int nBlockHeight = -1);
|
arith_uint256 CalculateScore(const uint256& blockHash);
|
||||||
|
|
||||||
bool UpdateFromNewBroadcast(CMasternodeBroadcast& mnb);
|
bool UpdateFromNewBroadcast(CMasternodeBroadcast& mnb);
|
||||||
|
|
||||||
|
@ -335,17 +335,22 @@ CMasternode* CMasternodeMan::GetNextMasternodeInQueueForPayment(int nBlockHeight
|
|||||||
// Sort them low to high
|
// Sort them low to high
|
||||||
sort(vecMasternodeLastPaid.begin(), vecMasternodeLastPaid.end(), CompareLastPaidBlock());
|
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
|
// 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
|
// -- 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
|
// -- 1/100 payments should be a double payment on mainnet - (1/(3000/10))*2
|
||||||
// -- (chance per block * chances before IsScheduled will fire)
|
// -- (chance per block * chances before IsScheduled will fire)
|
||||||
int nTenthNetwork = CountEnabled()/10;
|
int nTenthNetwork = CountEnabled()/10;
|
||||||
int nCountTenth = 0;
|
int nCountTenth = 0;
|
||||||
arith_uint256 nHigh = 0;
|
arith_uint256 nHighest = 0;
|
||||||
BOOST_FOREACH (PAIRTYPE(int, CMasternode*)& s, vecMasternodeLastPaid){
|
BOOST_FOREACH (PAIRTYPE(int, CMasternode*)& s, vecMasternodeLastPaid){
|
||||||
arith_uint256 n = UintToArith256(s.second->CalculateScore(nBlockHeight - 101));
|
arith_uint256 nScore = s.second->CalculateScore(blockHash);
|
||||||
if(n > nHigh){
|
if(nScore > nHighest){
|
||||||
nHigh = n;
|
nHighest = nScore;
|
||||||
pBestMasternode = s.second;
|
pBestMasternode = s.second;
|
||||||
}
|
}
|
||||||
nCountTenth++;
|
nCountTenth++;
|
||||||
@ -401,8 +406,8 @@ int CMasternodeMan::GetMasternodeRank(const CTxIn& vin, int64_t nBlockHeight, in
|
|||||||
std::vector<std::pair<int64_t, CMasternode*> > vecMasternodeScores;
|
std::vector<std::pair<int64_t, CMasternode*> > vecMasternodeScores;
|
||||||
|
|
||||||
//make sure we know about this block
|
//make sure we know about this block
|
||||||
uint256 hash = uint256();
|
uint256 blockHash = uint256();
|
||||||
if(!GetBlockHash(hash, nBlockHeight)) return -1;
|
if(!GetBlockHash(blockHash, nBlockHeight)) return -1;
|
||||||
|
|
||||||
LOCK(cs);
|
LOCK(cs);
|
||||||
|
|
||||||
@ -413,10 +418,9 @@ int CMasternodeMan::GetMasternodeRank(const CTxIn& vin, int64_t nBlockHeight, in
|
|||||||
mn.Check();
|
mn.Check();
|
||||||
if(!mn.IsEnabled()) continue;
|
if(!mn.IsEnabled()) continue;
|
||||||
}
|
}
|
||||||
uint256 n = mn.CalculateScore(nBlockHeight);
|
int64_t nScore = mn.CalculateScore(blockHash).GetCompact(false);
|
||||||
int64_t n2 = UintToArith256(n).GetCompact(false);
|
|
||||||
|
|
||||||
vecMasternodeScores.push_back(std::make_pair(n2, &mn));
|
vecMasternodeScores.push_back(std::make_pair(nScore, &mn));
|
||||||
}
|
}
|
||||||
|
|
||||||
sort(vecMasternodeScores.rbegin(), vecMasternodeScores.rend(), CompareScoreMN());
|
sort(vecMasternodeScores.rbegin(), vecMasternodeScores.rend(), CompareScoreMN());
|
||||||
@ -436,8 +440,8 @@ std::vector<pair<int, CMasternode> > CMasternodeMan::GetMasternodeRanks(int64_t
|
|||||||
std::vector<std::pair<int, CMasternode> > vecMasternodeRanks;
|
std::vector<std::pair<int, CMasternode> > vecMasternodeRanks;
|
||||||
|
|
||||||
//make sure we know about this block
|
//make sure we know about this block
|
||||||
uint256 hash = uint256();
|
uint256 blockHash = uint256();
|
||||||
if(!GetBlockHash(hash, nBlockHeight)) return vecMasternodeRanks;
|
if(!GetBlockHash(blockHash, nBlockHeight)) return vecMasternodeRanks;
|
||||||
|
|
||||||
LOCK(cs);
|
LOCK(cs);
|
||||||
|
|
||||||
@ -448,10 +452,9 @@ std::vector<pair<int, CMasternode> > CMasternodeMan::GetMasternodeRanks(int64_t
|
|||||||
|
|
||||||
if(mn.nProtocolVersion < minProtocol || !mn.IsEnabled()) continue;
|
if(mn.nProtocolVersion < minProtocol || !mn.IsEnabled()) continue;
|
||||||
|
|
||||||
uint256 n = mn.CalculateScore(nBlockHeight);
|
int64_t nScore = mn.CalculateScore(blockHash).GetCompact(false);
|
||||||
int64_t n2 = UintToArith256(n).GetCompact(false);
|
|
||||||
|
|
||||||
vecMasternodeScores.push_back(std::make_pair(n2, &mn));
|
vecMasternodeScores.push_back(std::make_pair(nScore, &mn));
|
||||||
}
|
}
|
||||||
|
|
||||||
sort(vecMasternodeScores.rbegin(), vecMasternodeScores.rend(), CompareScoreMN());
|
sort(vecMasternodeScores.rbegin(), vecMasternodeScores.rend(), CompareScoreMN());
|
||||||
@ -471,6 +474,12 @@ CMasternode* CMasternodeMan::GetMasternodeByRank(int nRank, int64_t nBlockHeight
|
|||||||
|
|
||||||
LOCK(cs);
|
LOCK(cs);
|
||||||
|
|
||||||
|
uint256 blockHash;
|
||||||
|
if(!GetBlockHash(blockHash, nBlockHeight)) {
|
||||||
|
LogPrintf("CMasternode::GetMasternodeByRank -- ERROR: GetBlockHash() failed at nBlockHeight %d\n", nBlockHeight);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
// Fill scores
|
// Fill scores
|
||||||
BOOST_FOREACH(CMasternode& mn, vMasternodes) {
|
BOOST_FOREACH(CMasternode& mn, vMasternodes) {
|
||||||
|
|
||||||
@ -480,10 +489,9 @@ CMasternode* CMasternodeMan::GetMasternodeByRank(int nRank, int64_t nBlockHeight
|
|||||||
if(!mn.IsEnabled()) continue;
|
if(!mn.IsEnabled()) continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint256 n = mn.CalculateScore(nBlockHeight);
|
int64_t nScore = mn.CalculateScore(blockHash).GetCompact(false);
|
||||||
int64_t n2 = UintToArith256(n).GetCompact(false);
|
|
||||||
|
|
||||||
vecMasternodeScores.push_back(std::make_pair(n2, &mn));
|
vecMasternodeScores.push_back(std::make_pair(nScore, &mn));
|
||||||
}
|
}
|
||||||
|
|
||||||
sort(vecMasternodeScores.rbegin(), vecMasternodeScores.rend(), CompareScoreMN());
|
sort(vecMasternodeScores.rbegin(), vecMasternodeScores.rend(), CompareScoreMN());
|
||||||
|
Loading…
Reference in New Issue
Block a user