10#include <test/util/random.h>
11#include <test/util/setup_common.h>
13#include <boost/test/unit_test.hpp>
25 std::vector<CScript> winners;
28 BOOST_CHECK(!cache.getWinners(prevblockhash, winners));
32 BOOST_CHECK(cache.getWinners(prevblockhash, winners));
36 for (
size_t i = 0; i < manualWinners.size(); i++) {
44 for (
auto it = std::next(winners.begin(), manualWinners.size());
45 it != winners.end(); it++) {
48 if (proof->getPayoutScript() == *it) {
61 double lowestRank = std::numeric_limits<double>::max();
77 const ProofRef &proof,
int expected) {
91 StakeContenderStatus::UNKNOWN, StakeContenderStatus::ACCEPTED,
92 StakeContenderStatus::IN_WINNER_SET,
93 StakeContenderStatus::ACCEPTED | StakeContenderStatus::IN_WINNER_SET};
127 std::vector<CScript> manualWinners = {
132 std::vector<ProofRef>
proofs;
133 for (
int i = 0; i < 4; i++) {
141 for (
int i = 0; i < 5; i++) {
150 for (
int p = 0;
p < 4;
p++) {
157 StakeContenderStatus::ACCEPTED |
158 StakeContenderStatus::IN_WINNER_SET));
163 cache.
add(pindex,
proofs[1], StakeContenderStatus::ACCEPTED));
168 cache.
add(pindex,
proofs[2], StakeContenderStatus::IN_WINNER_SET));
177 for (
const auto &proof :
proofs) {
180 !cache.
add(pindex, proof, StakeContenderStatus::ACCEPTED));
182 StakeContenderStatus::ACCEPTED |
183 StakeContenderStatus::IN_WINNER_SET));
185 !cache.
add(pindex, proof, StakeContenderStatus::IN_WINNER_SET));
199 for (
const auto &proof :
proofs) {
200 const auto &
payout = proof->getPayoutScript();
212 for (
const auto &proof :
proofs) {
220 for (
const auto &proof :
proofs) {
250 pindex = pindex->
pprev;
255 for (
const auto &proof :
proofs) {
256 manualWinners.push_back(proof->getPayoutScript());
261 for (
int i = 0; i < 5; i++) {
263 {proofs[0], proofs[1]});
264 for (
int p = 0;
p < 4;
p++) {
267 pindex = pindex->
pprev;
275 std::vector<ProofRef>
proofs;
276 for (
int i = 0; i < 10; i++) {
283 for (
int i = 0; i < 3; i++) {
286 for (
const auto &proof :
proofs) {
287 cache.
add(pindex, proof, StakeContenderStatus::IN_WINNER_SET);
290 pindex = pindex->
pprev;
294 for (
int height : {0, 10, 50, 90, 98}) {
331 for (
const auto &proof :
proofs) {
353 for (
int height : {102, 200, 1000, 1000000}) {
355 for (
size_t i = 0; i < 2; i++) {
356 for (
const auto &proof :
proofs) {
362 std::vector<CScript> winners;
365 pindex = pindex->
pprev;
#define Assert(val)
Identity function.
The block chain is a tree shaped structure starting with the genesis block at the root,...
CBlockIndex * pprev
pointer to the index of the predecessor of this block
BlockHash GetBlockHash() const
Serialized script, used inside transaction inputs and outputs.
Chainstate stores and provides an API to update our local knowledge of the current best chain.
const ProofId & getId() const
Cache to track stake contenders for recent blocks.
bool invalidate(const StakeContenderId &contenderId)
bool accept(const StakeContenderId &contenderId)
Helpers to set avalanche state of a contender.
size_t isEmpty() const
For tests.
bool reject(const StakeContenderId &contenderId)
bool addWinner(const CBlockIndex *pindex, const CScript &payoutScript)
Add a proof that should be treated as a winner (already finalized).
int getVoteStatus(const StakeContenderId &contenderId) const
Get contender acceptance state for avalanche voting.
void cleanup(const int minHeight)
bool add(const CBlockIndex *pindex, const ProofRef &proof, uint8_t status=StakeContenderStatus::UNKNOWN)
Add a proof to consider in staking rewards pre-consensus.
bool finalize(const StakeContenderId &contenderId)
bool getWinners(const BlockHash &prevblockhash, std::vector< CScript > &payouts) const
Get payout scripts of the winning proofs.
void push_back(const T &value)
ProofRef buildRandomProof(Chainstate &active_chainstate, uint32_t score, int height, const CKey &masterKey)
constexpr uint32_t MIN_VALID_PROOF_SCORE
Implement std::hash so RCUPtr can be used as a key for maps or sets.
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
static void CheckVoteStatus(StakeContenderCache &cache, const BlockHash &prevblockhash, const ProofRef &proof, int expected)
static void CheckWinners(StakeContenderCache &cache, const BlockHash &prevblockhash, std::vector< CScript > manualWinners, std::vector< ProofRef > avalancheWinners)
BOOST_AUTO_TEST_CASE(vote_status_tests)
A BlockHash is a unqiue identifier for a block.
StakeContenderIds are unique for each block to ensure that the peer polling for their acceptance has ...
double ComputeProofRewardRank(uint32_t proofScore)
To make sure the selection is properly weighted according to the proof score, we normalize the conten...