Bitcoin ABC 0.31.1
P2P Digital Currency
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 <cstdint>
20#include <functional>
21#include <utility>
22#include <vector>
23
24class CBlockIndex;
25
26namespace avalanche {
27
28class PeerManager;
29
30enum StakeContenderStatus : uint8_t {
32 // Set according to avalanche acceptance
33 ACCEPTED = (1 << 0),
34 // If set, this contender should be in the stake winner set
35 IN_WINNER_SET = (1 << 1),
36};
37
42 uint8_t status;
43 // Cache payout script and proof score because the peer manager does not
44 // track past-valid proofs.
46 uint32_t score;
47
48 StakeContenderCacheEntry(const BlockHash &_prevblockhash, int _blockheight,
49 const ProofId &_proofid, uint8_t _status,
50 const CScript &_payoutScriptPubkey,
51 uint32_t _score)
52 : prevblockhash(_prevblockhash), blockheight(_blockheight),
53 proofid(_proofid), status(_status),
54 payoutScriptPubkey(_payoutScriptPubkey), score(_score) {}
55
56 double computeRewardRank() const {
58 }
61 }
63 bool isInWinnerSet() const {
65 }
66};
67
71 std::vector<CScript> payoutScripts;
72
73 ManualWinners(const BlockHash &_prevblockhash, int _blockheight,
74 const std::vector<CScript> &_payoutScripts)
75 : prevblockhash(_prevblockhash), blockheight(_blockheight),
76 payoutScripts(_payoutScripts) {}
77};
78
82 return entry.getStakeContenderId();
83 }
84};
85
86struct by_stakecontenderid;
87struct by_prevblockhash;
88struct by_blockheight;
89
90namespace bmi = boost::multi_index;
91
97
98 using ContenderSet = boost::multi_index_container<
100 bmi::indexed_by<
101 // index by stake contender id
102 bmi::hashed_unique<bmi::tag<by_stakecontenderid>,
104 // index by prevblockhash
105 bmi::hashed_non_unique<
106 bmi::tag<by_prevblockhash>,
110 // index by block height
111 bmi::ordered_non_unique<
112 bmi::tag<by_blockheight>,
113 bmi::member<StakeContenderCacheEntry, int,
115
117
118 using ManualWinnersSet = boost::multi_index_container<
120 bmi::indexed_by<
121 // index by prevblockhash
122 bmi::hashed_unique<bmi::tag<by_prevblockhash>,
123 bmi::member<ManualWinners, BlockHash,
126 // index by block height
127 bmi::ordered_unique<
128 bmi::tag<by_blockheight>,
129 bmi::member<ManualWinners, int, &ManualWinners::blockheight>>>>;
130
132
133public:
135
136 void cleanup(const int requestedMinHeight);
137
141 bool isEmpty() const {
142 return (contenders.size() + manualWinners.size()) == 0;
143 }
144
148 bool add(const CBlockIndex *pindex, const ProofRef &proof,
149 uint8_t status = StakeContenderStatus::UNKNOWN);
150
154 void promoteToBlock(
155 const CBlockIndex *activeTip,
156 std::function<bool(const ProofId &proofid)> const &shouldPromote);
157
162 bool setWinners(const CBlockIndex *pindex,
163 const std::vector<CScript> &payoutScripts);
164
168 bool accept(const StakeContenderId &contenderId);
169 bool finalize(const StakeContenderId &contenderId);
170 bool reject(const StakeContenderId &contenderId);
171
177 int getVoteStatus(const StakeContenderId &contenderId,
178 BlockHash &prevblockhashout) const;
179
186 const BlockHash &prevblockhash, size_t maxPollable,
187 std::vector<StakeContenderId> &pollableContenders) const;
188
189 bool getWinners(const BlockHash &prevblockhash,
190 std::vector<std::pair<ProofId, CScript>> &winners) const;
191};
192
193} // namespace avalanche
194
195#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:424
Cache to track stake contenders for recent blocks.
bool getWinners(const BlockHash &prevblockhash, std::vector< std::pair< ProofId, CScript > > &winners) const
bool accept(const StakeContenderId &contenderId)
Helpers to set avalanche state of a contender.
void cleanup(const int requestedMinHeight)
size_t getPollableContenders(const BlockHash &prevblockhash, size_t maxPollable, std::vector< StakeContenderId > &pollableContenders) const
Get the best ranking contenders, accepted contenders ranking first.
bool reject(const StakeContenderId &contenderId)
bool setWinners(const CBlockIndex *pindex, const std::vector< CScript > &payoutScripts)
Set proof(s) that should be treated as winners (already finalized).
bool add(const CBlockIndex *pindex, const ProofRef &proof, uint8_t status=StakeContenderStatus::UNKNOWN)
Add a proof to consider in staking rewards pre-consensus.
void promoteToBlock(const CBlockIndex *activeTip, std::function< bool(const ProofId &proofid)> const &shouldPromote)
Promote cache entries to a the active chain tip.
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
int getVoteStatus(const StakeContenderId &contenderId, BlockHash &prevblockhashout) const
Get contender acceptance state for avalanche voting.
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)
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
ManualWinners(const BlockHash &_prevblockhash, int _blockheight, const 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) const
To make sure the selection is properly weighted according to the proof score, we normalize the conten...
result_type operator()(const StakeContenderCacheEntry &entry) const