Bitcoin ABC 0.30.7
P2P Digital Currency
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
23class PeerManager;
24
25enum StakeContenderStatus : uint8_t {
27 // Set according to avalanche acceptance
28 ACCEPTED = (1 << 0),
29 // If set, this contender should be in the stake winner set
30 IN_WINNER_SET = (1 << 1),
31};
32
37 uint8_t status;
38 // Cache payout script and proof score because the peer manager does not
39 // track past-valid proofs.
41 uint32_t score;
42
43 StakeContenderCacheEntry(const BlockHash &_prevblockhash, int _blockheight,
44 const ProofId &_proofid, uint8_t _status,
45 const CScript &_payoutScriptPubkey,
46 uint32_t _score)
47 : prevblockhash(_prevblockhash), blockheight(_blockheight),
48 proofid(_proofid), status(_status),
49 payoutScriptPubkey(_payoutScriptPubkey), score(_score) {}
50
51 double computeRewardRank() const {
53 }
56 }
58 bool isInWinnerSet() const {
60 }
61};
62
66 std::vector<CScript> payoutScripts;
67
68 ManualWinners(const BlockHash &_prevblockhash, int _blockheight,
69 const std::vector<CScript> &_payoutScripts)
70 : prevblockhash(_prevblockhash), blockheight(_blockheight),
71 payoutScripts(_payoutScripts) {}
72};
73
77 return entry.getStakeContenderId();
78 }
79};
80
81struct by_stakecontenderid;
82struct by_prevblockhash;
83struct by_blockheight;
84
85namespace bmi = boost::multi_index;
86
92
93 using ContenderSet = boost::multi_index_container<
95 bmi::indexed_by<
96 // index by stake contender id
97 bmi::hashed_unique<bmi::tag<by_stakecontenderid>,
99 // index by prevblockhash
100 bmi::hashed_non_unique<
101 bmi::tag<by_prevblockhash>,
105 // index by block height
106 bmi::ordered_non_unique<
107 bmi::tag<by_blockheight>,
108 bmi::member<StakeContenderCacheEntry, int,
110
112
113 using ManualWinnersSet = boost::multi_index_container<
115 bmi::indexed_by<
116 // index by prevblockhash
117 bmi::hashed_unique<bmi::tag<by_prevblockhash>,
118 bmi::member<ManualWinners, BlockHash,
121 // index by block height
122 bmi::ordered_unique<
123 bmi::tag<by_blockheight>,
124 bmi::member<ManualWinners, int, &ManualWinners::blockheight>>>>;
125
127
128public:
130
131 void cleanup(const int requestedMinHeight);
132
136 bool isEmpty() const {
137 return (contenders.size() + manualWinners.size()) == 0;
138 }
139
143 bool add(const CBlockIndex *pindex, const ProofRef &proof,
144 uint8_t status = StakeContenderStatus::UNKNOWN);
145
149 void promoteToBlock(const CBlockIndex *activeTip, PeerManager &pm);
150
155 bool setWinners(const CBlockIndex *pindex,
156 const std::vector<CScript> &payoutScripts);
157
161 bool accept(const StakeContenderId &contenderId);
162 bool finalize(const StakeContenderId &contenderId);
163 bool reject(const StakeContenderId &contenderId);
164 bool invalidate(const StakeContenderId &contenderId);
165
171 int getVoteStatus(const StakeContenderId &contenderId,
172 BlockHash &prevblockhashout) const;
173
177 bool getWinners(const BlockHash &prevblockhash,
178 std::vector<CScript> &payouts) const;
179};
180
181} // namespace avalanche
182
183#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 invalidate(const StakeContenderId &contenderId)
bool accept(const StakeContenderId &contenderId)
Helpers to set avalanche state of a contender.
void cleanup(const int requestedMinHeight)
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.
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)
bool getWinners(const BlockHash &prevblockhash, std::vector< CScript > &payouts) const
Get payout scripts of the winning proofs.
void promoteToBlock(const CBlockIndex *activeTip, PeerManager &pm)
Promote cache entries to a the active chain tip.
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)
To make sure the selection is properly weighted according to the proof score, we normalize the conten...
result_type operator()(const StakeContenderCacheEntry &entry) const