Skip to content

Commit

Permalink
[Refactor] Use arith_uint256 for masternode ranking computations
Browse files Browse the repository at this point in the history
  • Loading branch information
random-zebra committed May 5, 2021
1 parent 990072e commit b02a67b
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 15 deletions.
8 changes: 6 additions & 2 deletions src/budget/budgetproposal.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,17 @@ class CBudgetProposal
void Relay();

// compare proposals by proposal hash
inline bool operator>(const CBudgetProposal& other) const { return GetHash() > other.GetHash(); }
inline bool operator>(const CBudgetProposal& other) const
{
return UintToArith256(GetHash()) > UintToArith256(other.GetHash());
}
//
// compare proposals pointers by net yes count (solve tie with feeHash)
static inline bool PtrHigherYes(CBudgetProposal* a, CBudgetProposal* b)
{
const int netYes_a = a->GetYeas() - a->GetNays();
const int netYes_b = b->GetYeas() - b->GetNays();
if (netYes_a == netYes_b) return a->GetFeeTXHash() > b->GetFeeTXHash();
if (netYes_a == netYes_b) return UintToArith256(a->GetFeeTXHash()) > UintToArith256(b->GetFeeTXHash());
return netYes_a > netYes_b;
}

Expand Down
2 changes: 1 addition & 1 deletion src/budget/finalizedbudget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ bool CFinalizedBudget::operator>(const CFinalizedBudget& other) const
const int count = GetVoteCount();
const int otherCount = other.GetVoteCount();

if (count == otherCount) return GetFeeTXHash() > other.GetFeeTXHash();
if (count == otherCount) return UintToArith256(GetFeeTXHash()) > UintToArith256(other.GetFeeTXHash());

return count > otherCount;
}
Expand Down
5 changes: 4 additions & 1 deletion src/budget/finalizedbudget.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,10 @@ class CTxBudgetPayment
}

// compare payments by proposal hash
inline bool operator>(const CTxBudgetPayment& other) const { return nProposalHash > other.nProposalHash; }
inline bool operator>(const CTxBudgetPayment& other) const
{
return UintToArith256(nProposalHash) > UintToArith256(other.nProposalHash);
}

};

Expand Down
8 changes: 4 additions & 4 deletions src/masternode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,17 @@ 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(const uint256& hash) const
arith_uint256 CMasternode::CalculateScore(const uint256& hash) const
{
CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION);
ss << hash;
const uint256& hash2 = ss.GetHash();
const arith_uint256& hash2 = UintToArith256(ss.GetHash());

CHashWriter ss2(SER_GETHASH, PROTOCOL_VERSION);
ss2 << hash;
const uint256& aux = vin.prevout.hash + vin.prevout.n;
const arith_uint256& aux = UintToArith256(vin.prevout.hash) + vin.prevout.n;
ss2 << aux;
const uint256& hash3 = ss2.GetHash();
const arith_uint256& hash3 = UintToArith256(ss2.GetHash());

return (hash3 > hash2 ? hash3 - hash2 : hash2 - hash3);
}
Expand Down
2 changes: 1 addition & 1 deletion src/masternode.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class CMasternode : public CSignedMessage
return !(a.vin == b.vin);
}

uint256 CalculateScore(const uint256& hash) const;
arith_uint256 CalculateScore(const uint256& hash) const;

ADD_SERIALIZE_METHODS;

Expand Down
12 changes: 6 additions & 6 deletions src/masternodeman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,13 +504,13 @@ MasternodeRef CMasternodeMan::GetNextMasternodeInQueueForPayment(int nBlockHeigh
// -- (chance per block * chances before IsScheduled will fire)
int nTenthNetwork = nMnCount / 10;
int nCountTenth = 0;
uint256 nHigh;
arith_uint256 nHigh;
const uint256& hash = GetHashAtHeight(nBlockHeight - 101);
for (const auto& s: vecMasternodeLastPaid) {
const MasternodeRef pmn = s.second;
if (!pmn) break;

const uint256& n = pmn->CalculateScore(hash);
const arith_uint256& n = pmn->CalculateScore(hash);
if (n > nHigh) {
nHigh = n;
pBestMasternode = pmn;
Expand Down Expand Up @@ -563,7 +563,7 @@ int CMasternodeMan::GetMasternodeRank(const CTxIn& vin, int64_t nBlockHeight) co
{
const uint256& hash = GetHashAtHeight(nBlockHeight - 1);
// height outside range
if (!hash) return -1;
if (hash == UINT256_ZERO) return -1;

// scan for winner
int minProtocol = ActiveProtocol();
Expand Down Expand Up @@ -605,7 +605,7 @@ std::vector<std::pair<int64_t, MasternodeRef>> CMasternodeMan::GetMasternodeRank
std::vector<std::pair<int64_t, MasternodeRef>> vecMasternodeScores;
const uint256& hash = GetHashAtHeight(nBlockHeight - 1);
// height outside range
if (!hash) return vecMasternodeScores;
if (hash == UINT256_ZERO) return vecMasternodeScores;
{
LOCK(cs);
// scan for winner
Expand Down Expand Up @@ -813,7 +813,7 @@ int64_t CMasternodeMan::SecondsSincePayment(const MasternodeRef& mn, const CBloc
CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION);
ss << mn->vin;
ss << mn->sigTime;
uint256 hash = ss.GetHash();
const arith_uint256& hash = UintToArith256(ss.GetHash());

// return some deterministic value for unknown/unpaid but force it to be more than 30 days old
return month + hash.GetCompact(false);
Expand All @@ -832,7 +832,7 @@ int64_t CMasternodeMan::GetLastPaid(const MasternodeRef& mn, const CBlockIndex*
uint256 hash = ss.GetHash();

// use a deterministic offset to break a tie -- 2.5 minutes
int64_t nOffset = hash.GetCompact(false) % 150;
int64_t nOffset = UintToArith256(hash).GetCompact(false) % 150;

int nMnCount = CountEnabled() * 1.25;
for (int n = 0; n < nMnCount; n++) {
Expand Down

0 comments on commit b02a67b

Please sign in to comment.