Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/peermanager.cpp
Show First 20 Lines • Show All 980 Lines • ▼ Show 20 Lines | bool PeerManager::selectStakingRewardWinner(const CBlockIndex *pprev, | ||||
// The previous block time is capped to now for the unlikely event the | // The previous block time is capped to now for the unlikely event the | ||||
// previous block time is in the future. | // previous block time is in the future. | ||||
const int64_t maxRegistrationTime = | const int64_t maxRegistrationTime = | ||||
std::min(pprev->GetBlockTime(), GetTime()) - | std::min(pprev->GetBlockTime(), GetTime()) - | ||||
std::chrono::duration_cast<std::chrono::seconds>(2 * | std::chrono::duration_cast<std::chrono::seconds>(2 * | ||||
Peer::DANGLING_TIMEOUT) | Peer::DANGLING_TIMEOUT) | ||||
.count(); | .count(); | ||||
const int64_t recentRegistrationTime = | |||||
std::min(pprev->GetBlockTime(), GetTime()) - | |||||
std::chrono::duration_cast<std::chrono::seconds>(4 * | |||||
Peer::DANGLING_TIMEOUT) | |||||
.count(); | |||||
const BlockHash prevblockhash = pprev->GetBlockHash(); | const BlockHash prevblockhash = pprev->GetBlockHash(); | ||||
winners.clear(); | winners.clear(); | ||||
while (winners.size() < peers.size()) { | while (winners.size() < peers.size()) { | ||||
double bestRewardRank = std::numeric_limits<double>::max(); | double bestRewardRank = std::numeric_limits<double>::max(); | ||||
ProofRef selectedProof = ProofRef(); | ProofRef selectedProof = ProofRef(); | ||||
int64_t selectedProofRegistrationTime{0}; | |||||
uint256 bestRewardHash; | uint256 bestRewardHash; | ||||
for (const Peer &peer : peers) { | for (const Peer &peer : peers) { | ||||
if (!peer.proof) { | if (!peer.proof) { | ||||
// Should never happen, continue | // Should never happen, continue | ||||
continue; | continue; | ||||
} | } | ||||
Show All 34 Lines | while (winners.size() < peers.size()) { | ||||
(256.0 - | (256.0 - | ||||
std::log2(UintToArith256(proofRewardHash).getdouble())) / | std::log2(UintToArith256(proofRewardHash).getdouble())) / | ||||
peer.getScore(); | peer.getScore(); | ||||
// The best ranking is the lowest ranking value | // The best ranking is the lowest ranking value | ||||
if (proofRewardRank < bestRewardRank) { | if (proofRewardRank < bestRewardRank) { | ||||
bestRewardRank = proofRewardRank; | bestRewardRank = proofRewardRank; | ||||
selectedProof = peer.proof; | selectedProof = peer.proof; | ||||
selectedProofRegistrationTime = peer.registration_time.count(); | |||||
bestRewardHash = proofRewardHash; | bestRewardHash = proofRewardHash; | ||||
} | } | ||||
// Select the lowest reward hash then proofid in the unlikely case | // Select the lowest reward hash then proofid in the unlikely case | ||||
// of a collision. | // of a collision. | ||||
if (proofRewardRank == bestRewardRank && | if (proofRewardRank == bestRewardRank && | ||||
(proofRewardHash < bestRewardHash || | (proofRewardHash < bestRewardHash || | ||||
(proofRewardHash == bestRewardHash && | (proofRewardHash == bestRewardHash && | ||||
peer.getProofId() < selectedProof->getId()))) { | peer.getProofId() < selectedProof->getId()))) { | ||||
selectedProof = peer.proof; | selectedProof = peer.proof; | ||||
selectedProofRegistrationTime = peer.registration_time.count(); | |||||
bestRewardHash = proofRewardHash; | bestRewardHash = proofRewardHash; | ||||
} | } | ||||
} | } | ||||
if (!selectedProof) { | if (!selectedProof) { | ||||
// No winner | // No winner | ||||
break; | break; | ||||
} | } | ||||
winners.push_back(selectedProof->getPayoutScript()); | winners.push_back(selectedProof->getPayoutScript()); | ||||
if (!isFlaky(selectedProof->getId())) { | if (selectedProofRegistrationTime < recentRegistrationTime && | ||||
!isFlaky(selectedProof->getId())) { | |||||
break; | break; | ||||
} | } | ||||
} | } | ||||
return winners.size() > 0; | return winners.size() > 0; | ||||
} | } | ||||
bool PeerManager::isFlaky(const ProofId &proofid) const { | bool PeerManager::isFlaky(const ProofId &proofid) const { | ||||
▲ Show 20 Lines • Show All 258 Lines • Show Last 20 Lines |