TARAXA
pillar_votes.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 #include <shared_mutex>
5 
6 #include "vote/pillar_vote.hpp"
7 
8 namespace taraxa::pillar_chain {
9 
10 class PillarVotes {
11  public:
12  struct WeightVotes {
13  std::unordered_map<vote_hash_t, std::pair<std::shared_ptr<PillarVote>, uint64_t /* vote weight */>> votes;
14  uint64_t weight{0}; // votes accumulated weight
15  };
16 
17  struct PeriodVotes {
18  std::unordered_map<blk_hash_t, WeightVotes> pillar_block_votes;
19  std::unordered_map<addr_t, vote_hash_t> unique_voters;
20 
21  // threshold for pillar chain consensus - total votes count / 2 + 1
22  uint64_t threshold{0};
23  };
24 
25  public:
32  bool voteExists(const std::shared_ptr<PillarVote> vote) const;
33 
40  bool isUniqueVote(const std::shared_ptr<PillarVote> vote) const;
41 
48  bool periodDataInitialized(PbftPeriod period) const;
49 
56  void initializePeriodData(PbftPeriod period, uint64_t threshold);
57 
65  bool addVerifiedVote(const std::shared_ptr<PillarVote>& vote, uint64_t validator_vote_count);
66 
77  std::vector<std::shared_ptr<PillarVote>> getVerifiedVotes(PbftPeriod period, const blk_hash_t& pillar_block_hash,
78  bool above_threshold = false) const;
79 
85  void eraseVotes(PbftPeriod min_period);
86 
87  private:
88  // Votes for latest_pillar_block_.period - 1, latest_pillar_block_.period and potential +1 future pillar
89  // block period
90  std::map<PbftPeriod, PeriodVotes> votes_;
91  mutable std::shared_mutex mutex_;
92 };
93 
94 } // namespace taraxa::pillar_chain
Definition: pillar_votes.hpp:10
std::shared_mutex mutex_
Definition: pillar_votes.hpp:91
bool voteExists(const std::shared_ptr< PillarVote > vote) const
Checks if vote exists.
Definition: pillar_votes.cpp:5
uint64_t threshold
Definition: pillar_votes.hpp:22
std::vector< std::shared_ptr< PillarVote > > getVerifiedVotes(PbftPeriod period, const blk_hash_t &pillar_block_hash, bool above_threshold=false) const
Get all pillar block votes for specified pillar block.
Definition: pillar_votes.cpp:41
std::map< PbftPeriod, PeriodVotes > votes_
Definition: pillar_votes.hpp:90
std::unordered_map< blk_hash_t, WeightVotes > pillar_block_votes
Definition: pillar_votes.hpp:18
bool addVerifiedVote(const std::shared_ptr< PillarVote > &vote, uint64_t validator_vote_count)
Add a vote to the votes map.
Definition: pillar_votes.cpp:104
void initializePeriodData(PbftPeriod period, uint64_t threshold)
Initialize period data with period_two_t_plus_one.
Definition: pillar_votes.cpp:133
bool isUniqueVote(const std::shared_ptr< PillarVote > vote) const
Checks if vote is unique per period & validator.
Definition: pillar_votes.cpp:21
uint64_t weight
Definition: pillar_votes.hpp:14
void eraseVotes(PbftPeriod min_period)
Erases votes wit period < min_period.
Definition: pillar_votes.cpp:138
std::unordered_map< addr_t, vote_hash_t > unique_voters
Definition: pillar_votes.hpp:19
bool periodDataInitialized(PbftPeriod period) const
Checks if specified period data have been already initialized.
Definition: pillar_votes.cpp:99
std::unordered_map< vote_hash_t, std::pair< std::shared_ptr< PillarVote >, uint64_t > > votes
Definition: pillar_votes.hpp:13
Definition: pillar_votes.hpp:17
Definition: pillar_votes.hpp:12
Definition: pillar_block.hpp:16
EthBlockNumber PbftPeriod
Definition: types.hpp:24