TARAXA
vote_manager.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "common/util.hpp"
4 #include "common/vrf_wrapper.hpp"
7 #include "pbft/pbft_chain.hpp"
8 #include "vote/pbft_vote.hpp"
10 
11 namespace taraxa {
12 
17 class Network;
18 class SlashingManager;
19 class PbftVote;
20 struct PbftConfig;
21 struct FullNodeConfig;
22 
23 namespace network::tarcap {
24 class TaraxaPeer;
25 }
26 
30 class VoteManager {
31  public:
32  VoteManager(const FullNodeConfig& config, std::shared_ptr<DbStorage> db, std::shared_ptr<PbftChain> pbft_chain,
33  std::shared_ptr<final_chain::FinalChain> final_chain, std::shared_ptr<KeyManager> key_manager,
34  std::shared_ptr<SlashingManager> slashing_manager);
35  ~VoteManager() = default;
36  VoteManager(const VoteManager&) = delete;
37  VoteManager(VoteManager&&) = delete;
38  VoteManager& operator=(const VoteManager&) = delete;
40 
45  void setNetwork(std::weak_ptr<Network> network);
46 
53  bool addVerifiedVote(const std::shared_ptr<PbftVote>& vote);
54 
60  bool voteInVerifiedMap(std::shared_ptr<PbftVote> const& vote) const;
61 
66  std::pair<bool, std::shared_ptr<PbftVote>> isUniqueVote(const std::shared_ptr<PbftVote>& vote) const;
67 
72  std::vector<std::shared_ptr<PbftVote>> getVerifiedVotes() const;
73 
78  uint64_t getVerifiedVotesSize() const;
79 
84  void cleanupVotesByPeriod(PbftPeriod pbft_period);
85 
92  std::vector<std::shared_ptr<PbftVote>> getProposalVotes(PbftPeriod period, PbftRound round) const;
93 
100  std::optional<PbftRound> determineNewRound(PbftPeriod current_pbft_period, PbftRound current_pbft_round);
101 
111  void resetRewardVotes(PbftPeriod period, PbftRound round, PbftStep step, const blk_hash_t& block_hash, Batch& batch);
112 
120  std::pair<bool, std::vector<std::shared_ptr<PbftVote>>> checkRewardVotes(const std::shared_ptr<PbftBlock>& pbft_block,
121  bool copy_votes);
122 
128  std::vector<std::shared_ptr<PbftVote>> getRewardVotes();
129 
136 
142  void saveOwnVerifiedVote(const std::shared_ptr<PbftVote>& vote);
143 
147  std::vector<std::shared_ptr<PbftVote>> getOwnVerifiedVotes();
148 
154  void clearOwnVerifiedVotes(Batch& write_batch);
155 
165  std::shared_ptr<PbftVote> generateVoteWithWeight(const blk_hash_t& blockhash, PbftVoteTypes vote_type,
166  PbftPeriod period, PbftRound round, PbftStep step);
167 
177  std::shared_ptr<PbftVote> generateVote(const blk_hash_t& blockhash, PbftVoteTypes type, PbftPeriod period,
178  PbftRound round, PbftStep step);
179 
187  std::pair<bool, std::string> validateVote(const std::shared_ptr<PbftVote>& vote, bool strict = true) const;
188 
195  std::optional<uint64_t> getPbftTwoTPlusOne(PbftPeriod pbft_period, PbftVoteTypes vote_type) const;
196 
201  bool voteAlreadyValidated(const vote_hash_t& vote_hash) const;
202 
207  bool genAndValidateVrfSortition(PbftPeriod pbft_period, PbftRound pbft_round) const;
208 
217  std::optional<blk_hash_t> getTwoTPlusOneVotedBlock(PbftPeriod period, PbftRound round,
218  TwoTPlusOneVotedBlockType type) const;
219 
228  std::vector<std::shared_ptr<PbftVote>> getTwoTPlusOneVotedBlockVotes(PbftPeriod period, PbftRound round,
229  TwoTPlusOneVotedBlockType type) const;
230 
238  void setCurrentPbftPeriodAndRound(PbftPeriod pbft_period, PbftRound pbft_round);
239 
250 
251  private:
256  bool isValidRewardVote(const std::shared_ptr<PbftVote>& vote) const;
257 
263  std::pair<bool, std::shared_ptr<PbftVote>> insertUniqueVote(const std::shared_ptr<PbftVote>& vote);
264 
271  uint64_t getPbftSortitionThreshold(uint64_t total_dpos_votes_count, PbftVoteTypes vote_type) const;
272 
273  private:
279 
280  std::shared_ptr<DbStorage> db_;
281  std::shared_ptr<PbftChain> pbft_chain_;
282  std::shared_ptr<final_chain::FinalChain> final_chain_;
283  std::shared_ptr<KeyManager> key_manager_;
284  std::weak_ptr<Network> network_;
285  std::shared_ptr<SlashingManager> slashing_manager_;
286 
287  // Current pbft period based on pbft_manager
288  std::atomic<PbftPeriod> current_pbft_period_{0};
289  // Current pbft round based on pbft_manager
290  std::atomic<PbftRound> current_pbft_round_{0};
291 
292  // Main storage for all verified votes
293  std::map<PbftPeriod, std::map<PbftRound, VerifiedVotes>> verified_votes_;
294  mutable std::shared_mutex verified_votes_access_;
295 
296  // Reward votes related info
300  std::vector<vote_hash_t> extra_reward_votes_;
301  mutable std::shared_mutex reward_votes_info_mutex_;
302 
303  // Own votes generated during current period & round
304  std::vector<std::shared_ptr<PbftVote>> own_verified_votes_;
305 
306  // Cache for current 2T+1 - <Vote type, <period, two_t_plus_one for period>>
307  // !!! Important: do not access it directly as it is not updated automatically, always call getPbftTwoTPlusOne instead
308  // !!!
309  mutable std::unordered_map<PbftVoteTypes, std::pair<PbftPeriod, uint64_t>> current_two_t_plus_one_;
310  mutable std::shared_mutex current_two_t_plus_one_mutex_;
311 
312  // Votes that have been already validated in terms of signature, stake, etc...
313  // It is used as protection against ddos attack so we do no validate/process vote more than once
315 
317 };
318 
321 } // namespace taraxa
Definition: util.hpp:202
void resetRewardVotes(PbftPeriod period, PbftRound round, PbftStep step, const blk_hash_t &block_hash, Batch &batch)
Replace current reward votes with new period, round & block hash based on vote.
Definition: vote_manager.cpp:583
std::optional< PbftRound > determineNewRound(PbftPeriod current_pbft_period, PbftRound current_pbft_round)
Check if there are enough next voting type votes to set PBFT to a forward round within period.
Definition: vote_manager.cpp:544
~VoteManager()=default
std::shared_mutex reward_votes_info_mutex_
Definition: vote_manager.hpp:301
bool addVerifiedVote(const std::shared_ptr< PbftVote > &vote)
Add a vote to the verified votes map.
Definition: vote_manager.cpp:186
std::shared_ptr< final_chain::FinalChain > final_chain_
Definition: vote_manager.hpp:282
std::pair< bool, std::vector< std::shared_ptr< PbftVote > > > checkRewardVotes(const std::shared_ptr< PbftBlock > &pbft_block, bool copy_votes)
Check reward votes for specified pbft block.
Definition: vote_manager.cpp:678
bool voteAlreadyValidated(const vote_hash_t &vote_hash) const
Definition: vote_manager.cpp:963
std::optional< uint64_t > getPbftTwoTPlusOne(PbftPeriod pbft_period, PbftVoteTypes vote_type) const
Get 2t+1. 2t+1 is 2/3 of PBFT sortition threshold and plus 1 for a specific period.
Definition: vote_manager.cpp:929
std::vector< vote_hash_t > extra_reward_votes_
Definition: vote_manager.hpp:300
blk_hash_t reward_votes_block_hash_
Definition: vote_manager.hpp:297
const vrf_wrapper::vrf_sk_t kVrfSk
Definition: vote_manager.hpp:276
std::pair< bool, std::shared_ptr< PbftVote > > isUniqueVote(const std::shared_ptr< PbftVote > &vote) const
Definition: vote_manager.cpp:372
std::shared_ptr< PbftVote > generateVote(const blk_hash_t &blockhash, PbftVoteTypes type, PbftPeriod period, PbftRound round, PbftStep step)
Generate a vote.
Definition: vote_manager.cpp:870
VoteManager(const VoteManager &)=delete
std::vector< std::shared_ptr< PbftVote > > getVerifiedVotes() const
Get all verified votes.
Definition: vote_manager.cpp:78
const addr_t kNodeAddr
Definition: vote_manager.hpp:274
VoteManager & operator=(const VoteManager &)=delete
std::shared_ptr< DbStorage > db_
Definition: vote_manager.hpp:280
const PbftConfig & kPbftConfig
Definition: vote_manager.hpp:275
const secret_t kNodeSk
Definition: vote_manager.hpp:277
std::atomic< PbftPeriod > current_pbft_period_
Definition: vote_manager.hpp:288
bool voteInVerifiedMap(std::shared_ptr< PbftVote > const &vote) const
Check if the vote has been in the verified votes map.
Definition: vote_manager.cpp:346
void setNetwork(std::weak_ptr< Network > network)
Set network as a weak pointer.
Definition: vote_manager.cpp:76
void clearOwnVerifiedVotes(Batch &write_batch)
Clear own verified votes.
Definition: vote_manager.cpp:816
uint64_t getPbftSortitionThreshold(uint64_t total_dpos_votes_count, PbftVoteTypes vote_type) const
Get PBFT sortition threshold for specific period.
Definition: vote_manager.cpp:821
VoteManager & operator=(VoteManager &&)=delete
std::shared_ptr< PbftChain > pbft_chain_
Definition: vote_manager.hpp:281
std::vector< std::shared_ptr< PbftVote > > getOwnVerifiedVotes()
Definition: vote_manager.cpp:814
std::shared_ptr< KeyManager > key_manager_
Definition: vote_manager.hpp:283
std::shared_ptr< SlashingManager > slashing_manager_
Definition: vote_manager.hpp:285
void saveOwnVerifiedVote(const std::shared_ptr< PbftVote > &vote)
Saves own verified vote into memory and db.
Definition: vote_manager.cpp:809
std::shared_mutex verified_votes_access_
Definition: vote_manager.hpp:294
VoteManager(VoteManager &&)=delete
std::pair< bool, std::shared_ptr< PbftVote > > insertUniqueVote(const std::shared_ptr< PbftVote > &vote)
Inserts unique vote.
Definition: vote_manager.cpp:436
std::vector< std::shared_ptr< PbftVote > > getProposalVotes(PbftPeriod period, PbftRound round) const
Get all verified votes in proposal vote type for the current PBFT round.
Definition: vote_manager.cpp:515
void setCurrentPbftPeriodAndRound(PbftPeriod pbft_period, PbftRound pbft_round)
Sets current pbft period & round. It also checks if we dont already have 2t+1 vote bundles(pf any typ...
Definition: vote_manager.cpp:115
std::unordered_map< PbftVoteTypes, std::pair< PbftPeriod, uint64_t > > current_two_t_plus_one_
Definition: vote_manager.hpp:309
bool genAndValidateVrfSortition(PbftPeriod pbft_period, PbftRound pbft_round) const
Generates vrf sortition and calculates its weight.
Definition: vote_manager.cpp:967
std::weak_ptr< Network > network_
Definition: vote_manager.hpp:284
const dev::Public kNodePub
Definition: vote_manager.hpp:278
std::vector< std::shared_ptr< PbftVote > > own_verified_votes_
Definition: vote_manager.hpp:304
std::atomic< PbftRound > current_pbft_round_
Definition: vote_manager.hpp:290
ExpirationCache< vote_hash_t > already_validated_votes_
Definition: vote_manager.hpp:314
VoteManager(const FullNodeConfig &config, std::shared_ptr< DbStorage > db, std::shared_ptr< PbftChain > pbft_chain, std::shared_ptr< final_chain::FinalChain > final_chain, std::shared_ptr< KeyManager > key_manager, std::shared_ptr< SlashingManager > slashing_manager)
Definition: vote_manager.cpp:14
std::vector< std::shared_ptr< PbftVote > > getRewardVotes()
Get reward votes with the round during which was the previous block pushed.
Definition: vote_manager.cpp:784
std::vector< std::shared_ptr< PbftVote > > getTwoTPlusOneVotedBlockVotes(PbftPeriod period, PbftRound round, TwoTPlusOneVotedBlockType type) const
Definition: vote_manager.cpp:1020
uint64_t getVerifiedVotesSize() const
Get the total size of all verified votes.
Definition: vote_manager.cpp:98
std::pair< bool, std::string > validateVote(const std::shared_ptr< PbftVote > &vote, bool strict=true) const
Validates vote.
Definition: vote_manager.cpp:877
std::shared_ptr< PbftVote > generateVoteWithWeight(const blk_hash_t &blockhash, PbftVoteTypes vote_type, PbftPeriod period, PbftRound round, PbftStep step)
Place a vote, save it in the verified votes queue, and gossip to peers.
Definition: vote_manager.cpp:833
PbftStep getNetworkTplusOneNextVotingStep(PbftPeriod period, PbftRound round) const
Returns greatest step (in specified period & round), for which there is at least t+1 voting power fro...
Definition: vote_manager.cpp:170
std::map< PbftPeriod, std::map< PbftRound, VerifiedVotes > > verified_votes_
Definition: vote_manager.hpp:293
PbftPeriod getRewardVotesPbftBlockPeriod()
Get current reward votes pbft block period.
Definition: vote_manager.cpp:578
PbftRound reward_votes_round_
Definition: vote_manager.hpp:299
bool isValidRewardVote(const std::shared_ptr< PbftVote > &vote) const
Definition: vote_manager.cpp:645
std::optional< blk_hash_t > getTwoTPlusOneVotedBlock(PbftPeriod period, PbftRound round, TwoTPlusOneVotedBlockType type) const
Get 2t+1 voted block for specific period, round and type, e.g. soft/cert/next voted block.
Definition: vote_manager.cpp:998
PbftRound reward_votes_period_
Definition: vote_manager.hpp:298
std::shared_mutex current_two_t_plus_one_mutex_
Definition: vote_manager.hpp:310
void cleanupVotesByPeriod(PbftPeriod pbft_period)
Cleanup votes for previous PBFT periods.
Definition: vote_manager.cpp:506
VoteManager class manage votes for PBFT consensus.
Definition: vote_manager.hpp:30
PbftVoteTypes
Definition: vrf_sortition.hpp:21
#define LOG_OBJECTS_DEFINE
Definition: logger.hpp:60
Definition: config.hpp:8
rocksdb::WriteBatch Batch
Definition: storage.hpp:73
uint32_t PbftStep
Definition: types.hpp:26
uint32_t PbftRound
Definition: types.hpp:25
EthBlockNumber PbftPeriod
Definition: types.hpp:24
TwoTPlusOneVotedBlockType
Definition: verified_votes.hpp:11
Definition: config.hpp:24
Definition: pbft_config.hpp:9