TARAXA
Loading...
Searching...
No Matches
verified_votes.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <shared_mutex>
4#include <unordered_map>
5
6#include "common/types.hpp"
7#include "logger/logger.hpp"
8
9namespace taraxa {
10
11class PbftVote;
12
14
19
20using TwoTVotedBlockMap = std::unordered_map<TwoTPlusOneVotedBlockType, VotedBlock>;
21
23 uint64_t weight;
24 std::unordered_map<vote_hash_t, std::shared_ptr<PbftVote>> votes;
25};
26
27using UniqueVotersMap = std::unordered_map<addr_t, std::pair<std::shared_ptr<PbftVote>, std::shared_ptr<PbftVote>>>;
28
29struct StepVotes {
30 std::unordered_map<blk_hash_t, VotesWithWeight> votes;
32};
33using StepVotesMap = std::map<PbftStep, StepVotes>;
34
36 // 2t+1 voted blocks
38 // Step votes
40
41 // Greatest step, for which there is at least t+1 next votes - it is used for lambda exponential backoff: Usually
42 // when network gets stalled it is due to lack of 2t+1 voting power and steps keep increasing. When new node joins
43 // the network, it should catch up with the rest of nodes asap so we dont start exponentially backing of its lambda
44 // if it's current step is far behind network_t_plus_one_step (at least 1 third of network is at this step)
46};
47
48using RoundVerifiedVotesMap = std::map<PbftRound, RoundVerifiedVotes>;
49using PeriodVerifiedVotesMap = std::map<PbftPeriod, RoundVerifiedVotesMap>;
50
52 public:
53 VerifiedVotes(addr_t node_addr) { LOG_OBJECTS_CREATE("VERIFIED_VOTES"); }
54
55 uint64_t size() const;
56 std::vector<std::shared_ptr<PbftVote>> votes() const;
57
58 std::optional<const RoundVerifiedVotesMap> getPeriodVotes(PbftPeriod period) const;
59 std::optional<const RoundVerifiedVotes> getRoundVotes(PbftPeriod period, PbftRound round) const;
60 std::optional<const StepVotes> getStepVotes(PbftPeriod period, PbftRound round, PbftStep step) const;
61
62 std::optional<VotedBlock> getTwoTPlusOneVotedBlock(PbftPeriod period, PbftRound round,
63 TwoTPlusOneVotedBlockType type) const;
64 std::vector<std::shared_ptr<PbftVote>> getTwoTPlusOneVotedBlockVotes(PbftPeriod period, PbftRound round,
65 TwoTPlusOneVotedBlockType type) const;
66 void cleanupVotesByPeriod(PbftPeriod pbft_period);
67
68 std::optional<std::shared_ptr<PbftVote>> insertUniqueVoter(const std::shared_ptr<PbftVote>& vote);
69
70 std::optional<VotesWithWeight> insertVotedValue(const std::shared_ptr<PbftVote>& vote);
71
72 void setNetworkTPlusOneStep(std::shared_ptr<PbftVote> vote);
73 // Insert new 2t+1 voted block
74 void insertTwoTPlusOneVotedBlock(TwoTPlusOneVotedBlockType type, std::shared_ptr<PbftVote> vote);
75
76 private:
77 std::pair<bool, PeriodVerifiedVotesMap::iterator> get(PbftPeriod period);
78 std::pair<bool, PeriodVerifiedVotesMap::const_iterator> get(PbftPeriod period) const;
79 std::pair<bool, RoundVerifiedVotesMap::iterator> get(PbftPeriod period, PbftRound round);
80 std::pair<bool, RoundVerifiedVotesMap::const_iterator> get(PbftPeriod period, PbftRound round) const;
81 std::pair<bool, StepVotesMap::iterator> get(PbftPeriod period, PbftRound round, PbftStep step);
82 std::pair<bool, StepVotesMap::const_iterator> get(PbftPeriod period, PbftRound round, PbftStep step) const;
83
84 mutable std::shared_mutex verified_votes_access_;
86
88};
89
90} // namespace taraxa
Definition verified_votes.hpp:51
uint64_t size() const
Definition verified_votes.cpp:8
std::vector< std::shared_ptr< PbftVote > > getTwoTPlusOneVotedBlockVotes(PbftPeriod period, PbftRound round, TwoTPlusOneVotedBlockType type) const
Definition verified_votes.cpp:110
std::pair< bool, StepVotesMap::iterator > get(PbftPeriod period, PbftRound round, PbftStep step)
std::vector< std::shared_ptr< PbftVote > > votes() const
Definition verified_votes.cpp:23
VerifiedVotes(addr_t node_addr)
Definition verified_votes.hpp:53
std::optional< VotesWithWeight > insertVotedValue(const std::shared_ptr< PbftVote > &vote)
Definition verified_votes.cpp:246
std::optional< const RoundVerifiedVotes > getRoundVotes(PbftPeriod period, PbftRound round) const
Definition verified_votes.cpp:53
std::pair< bool, PeriodVerifiedVotesMap::const_iterator > get(PbftPeriod period) const
PeriodVerifiedVotesMap verified_votes_
Definition verified_votes.hpp:85
std::optional< std::shared_ptr< PbftVote > > insertUniqueVoter(const std::shared_ptr< PbftVote > &vote)
Definition verified_votes.cpp:174
void setNetworkTPlusOneStep(std::shared_ptr< PbftVote > vote)
Definition verified_votes.cpp:138
void insertTwoTPlusOneVotedBlock(TwoTPlusOneVotedBlockType type, std::shared_ptr< PbftVote > vote)
Definition verified_votes.cpp:154
std::optional< VotedBlock > getTwoTPlusOneVotedBlock(PbftPeriod period, PbftRound round, TwoTPlusOneVotedBlockType type) const
Definition verified_votes.cpp:88
std::pair< bool, PeriodVerifiedVotesMap::iterator > get(PbftPeriod period)
void cleanupVotesByPeriod(PbftPeriod pbft_period)
Definition verified_votes.cpp:286
std::optional< const RoundVerifiedVotesMap > getPeriodVotes(PbftPeriod period) const
Definition verified_votes.cpp:43
std::shared_mutex verified_votes_access_
Definition verified_votes.hpp:84
std::pair< bool, StepVotesMap::const_iterator > get(PbftPeriod period, PbftRound round, PbftStep step) const
std::pair< bool, RoundVerifiedVotesMap::const_iterator > get(PbftPeriod period, PbftRound round) const
std::optional< const StepVotes > getStepVotes(PbftPeriod period, PbftRound round, PbftStep step) const
Definition verified_votes.cpp:68
std::pair< bool, RoundVerifiedVotesMap::iterator > get(PbftPeriod period, PbftRound round)
#define LOG_OBJECTS_DEFINE
Definition logger.hpp:60
#define LOG_OBJECTS_CREATE(channel)
Definition logger.hpp:68
Definition app.hpp:16
std::unordered_map< blk_hash_t, VotesWithWeight > votes
Definition verified_votes.hpp:30
PbftStep network_t_plus_one_step
Definition verified_votes.hpp:45
uint32_t PbftStep
Definition types.hpp:26
std::map< PbftRound, RoundVerifiedVotes > RoundVerifiedVotesMap
Definition verified_votes.hpp:48
std::unordered_map< addr_t, std::pair< std::shared_ptr< PbftVote >, std::shared_ptr< PbftVote > > > UniqueVotersMap
Definition verified_votes.hpp:27
std::map< PbftStep, StepVotes > StepVotesMap
Definition verified_votes.hpp:33
uint32_t PbftRound
Definition types.hpp:25
EthBlockNumber PbftPeriod
Definition types.hpp:24
PbftStep step
Definition verified_votes.hpp:17
std::unordered_map< vote_hash_t, std::shared_ptr< PbftVote > > votes
Definition verified_votes.hpp:24
std::map< PbftPeriod, RoundVerifiedVotesMap > PeriodVerifiedVotesMap
Definition verified_votes.hpp:49
uint64_t weight
Definition verified_votes.hpp:23
std::unordered_map< TwoTPlusOneVotedBlockType, VotedBlock > TwoTVotedBlockMap
Definition verified_votes.hpp:20
blk_hash_t hash
Definition verified_votes.hpp:16
TwoTVotedBlockMap two_t_plus_one_voted_blocks_
Definition verified_votes.hpp:37
UniqueVotersMap unique_voters
Definition verified_votes.hpp:31
TwoTPlusOneVotedBlockType
Definition verified_votes.hpp:13
StepVotesMap step_votes
Definition verified_votes.hpp:39
Definition verified_votes.hpp:35
Definition verified_votes.hpp:29
Definition verified_votes.hpp:15
Definition verified_votes.hpp:22