Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
stakecontendercache.h
Go to the documentation of this file.
1// Copyright (c) 2024 The Bitcoin developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#ifndef BITCOIN_AVALANCHE_STAKECONTENDERCACHE_H
6#define BITCOIN_AVALANCHE_STAKECONTENDERCACHE_H
7
8#include <avalanche/proof.h>
11#include <script/script.h>
12#include <util/hasher.h>
13
14#include <boost/multi_index/hashed_index.hpp>
15#include <boost/multi_index/member.hpp>
16#include <boost/multi_index/ordered_index.hpp>
17#include <boost/multi_index_container.hpp>
18
19#include <vector>
20
21namespace avalanche {
22
25 // Set according to avalanche acceptance
26 ACCEPTED = (1 << 0),
27 // If set, this contender should be in the stake winner set
28 IN_WINNER_SET = (1 << 1),
29};
30
60
71
78
80struct by_prevblockhash;
81struct by_blockheight;
82
83namespace bmi = boost::multi_index;
84
89 using ContenderSet = boost::multi_index_container<
91 bmi::indexed_by<
92 // index by stake contender id
93 bmi::hashed_unique<bmi::tag<by_stakecontenderid>,
95 // index by prevblockhash
96 bmi::hashed_non_unique<
97 bmi::tag<by_prevblockhash>,
101 // index by block height
102 bmi::ordered_non_unique<
103 bmi::tag<by_blockheight>,
104 bmi::member<StakeContenderCacheEntry, int,
106
108
109 using ManualWinnersSet = boost::multi_index_container<
111 bmi::indexed_by<
112 // index by prevblockhash
113 bmi::hashed_unique<bmi::tag<by_prevblockhash>,
114 bmi::member<ManualWinners, BlockHash,
117 // index by block height
118 bmi::ordered_unique<
119 bmi::tag<by_blockheight>,
120 bmi::member<ManualWinners, int, &ManualWinners::blockheight>>>>;
121
123
124public:
126
127 void cleanup(const int minHeight);
128
132 size_t isEmpty() const {
133 return (contenders.size() + manualWinners.size()) == 0;
134 }
135
139 bool add(const CBlockIndex *pindex, const ProofRef &proof,
145 bool addWinner(const CBlockIndex *pindex, const CScript &payoutScript);
146
154
160
164 bool getWinners(const BlockHash &prevblockhash,
165 std::vector<CScript> &payouts) const;
166};
167
168} // namespace avalanche
169
170#endif // BITCOIN_AVALANCHE_STAKECONTENDERCACHE_H
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition blockindex.h:25
Serialized script, used inside transaction inputs and outputs.
Definition script.h:431
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.
boost::multi_index_container< StakeContenderCacheEntry, bmi::indexed_by< bmi::hashed_unique< bmi::tag< by_stakecontenderid >, stakecontenderid_index, SaltedUint256Hasher >, bmi::hashed_non_unique< bmi::tag< by_prevblockhash >, bmi::member< StakeContenderCacheEntry, BlockHash, &StakeContenderCacheEntry::prevblockhash >, SaltedUint256Hasher >, bmi::ordered_non_unique< bmi::tag< by_blockheight >, bmi::member< StakeContenderCacheEntry, int, &StakeContenderCacheEntry::blockheight > > > > ContenderSet
boost::multi_index_container< ManualWinners, bmi::indexed_by< bmi::hashed_unique< bmi::tag< by_prevblockhash >, bmi::member< ManualWinners, BlockHash, &ManualWinners::prevblockhash >, SaltedUint256Hasher >, bmi::ordered_unique< bmi::tag< by_blockheight >, bmi::member< ManualWinners, int, &ManualWinners::blockheight > > > > ManualWinnersSet
bool finalize(const StakeContenderId &contenderId)
bool getWinners(const BlockHash &prevblockhash, std::vector< CScript > &payouts) const
Get payout scripts of the winning proofs.
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...
Definition random.h:85
A BlockHash is a unqiue identifier for a block.
Definition blockhash.h:13
ManualWinners(const BlockHash &_prevblockhash, int _blockheight, std::vector< CScript > &_payoutScripts)
std::vector< CScript > payoutScripts
bool isAccepted() const
CScript payoutScriptPubkey
BlockHash prevblockhash
bool isInWinnerSet() const
uint32_t score
uint8_t status
ProofId proofid
int blockheight
double computeRewardRank() const
StakeContenderCacheEntry(const BlockHash &_prevblockhash, int _blockheight, const ProofId &_proofid, uint8_t _status, const CScript &_payoutScriptPubkey, uint32_t _score)
StakeContenderId getStakeContenderId() const
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...
result_type operator()(const StakeContenderCacheEntry &entry) const