TARAXA
pbft_vote.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <json/value.h>
4 
5 #include "common/vrf_wrapper.hpp"
6 #include "vote.hpp"
7 #include "vrf_sortition.hpp"
8 
9 namespace taraxa {
10 
19 class PbftVote : public Vote {
20  public:
22  PbftVote() = default;
23  PbftVote(secret_t const& node_sk, VrfPbftSortition vrf_sortition, blk_hash_t const& block_hash);
24 
25  // Ctor for optimized rlp vote objects - only signature and vrf proof are in the rlp
26  explicit PbftVote(const blk_hash_t& block_hash, PbftPeriod period, PbftRound round, PbftStep step,
27  dev::RLP const& rlp);
28 
29  // Ctors for full rlp vote objects - all data are encoded in the rlp
30  explicit PbftVote(dev::RLP const& rlp);
31  explicit PbftVote(bytes const& rlp);
32 
33  bool operator==(const PbftVote& other) const;
34 
39  bool verifyVrfSortition(const vrf_pk_t& pk, bool strict) const;
40 
45  const VrfPbftSortition& getVrfSortition() const;
46 
52 
58 
63  PbftVoteTypes getType() const;
64 
69  PbftPeriod getPeriod() const;
70 
75  PbftRound getRound() const;
76 
81  PbftStep getStep() const;
82 
89  bytes rlp(bool inc_sig = true, bool inc_weight = false) const;
90 
97  bytes optimizedRlp() const;
98 
106  uint64_t calculateWeight(uint64_t stake, double dpos_total_votes_count, double threshold) const;
107 
112  std::optional<uint64_t> getWeight() const;
113 
114  friend std::ostream& operator<<(std::ostream& strm, PbftVote const& vote) {
115  strm << "[Vote] " << std::endl;
116  strm << " vote_hash: " << vote.vote_hash_ << std::endl;
117  strm << " voter: " << vote.getVoter() << std::endl;
118  strm << " vote_signature: " << vote.vote_signature_ << std::endl;
119  strm << " blockhash: " << vote.block_hash_ << std::endl;
120  if (vote.weight_) strm << " weight: " << vote.weight_.value() << std::endl;
121  strm << " vrf_sortition: " << vote.vrf_sortition_ << std::endl;
122  return strm;
123  }
124 
129  Json::Value toJSON() const;
130 
131  private:
137  vote_hash_t sha3(bool inc_sig) const override;
138 
140  mutable std::optional<uint64_t> weight_;
141 };
142 
145 } // namespace taraxa
Definition: RLP.h:56
bytes rlp(bool inc_sig=true, bool inc_weight=false) const
Recursive Length Prefix.
Definition: pbft_vote.cpp:56
VrfPbftSortition vrf_sortition_
Definition: pbft_vote.hpp:139
bool verifyVrfSortition(const vrf_pk_t &pk, bool strict) const
Verify VRF sortition.
Definition: pbft_vote.cpp:101
bytes optimizedRlp() const
Optimized Recursive Length Prefix.
Definition: pbft_vote.cpp:80
vote_hash_t sha3(bool inc_sig) const override
Secure Hash Algorithm 3.
Definition: pbft_vote.cpp:125
Json::Value toJSON() const
Get vote in JSON representation.
Definition: pbft_vote.cpp:88
std::optional< uint64_t > weight_
Definition: pbft_vote.hpp:140
friend std::ostream & operator<<(std::ostream &strm, PbftVote const &vote)
Definition: pbft_vote.hpp:114
PbftStep getStep() const
Get vote PBFT step.
Definition: pbft_vote.cpp:123
const VrfPbftSortition & getVrfSortition() const
Get VRF sortition.
Definition: pbft_vote.cpp:103
const public_t & getVoter() const
Get voter public key.
Definition: vote.cpp:20
std::optional< uint64_t > getWeight() const
Get vote weight.
Definition: pbft_vote.cpp:113
uint64_t calculateWeight(uint64_t stake, double dpos_total_votes_count, double threshold) const
Calculate vote weight.
Definition: pbft_vote.cpp:107
PbftVote()=default
PbftVoteTypes getType() const
Get vote type.
Definition: pbft_vote.cpp:117
PbftPeriod getPeriod() const
Get vote PBFT period.
Definition: pbft_vote.cpp:119
PbftRound getRound() const
Get vote PBFT round.
Definition: pbft_vote.cpp:121
bool operator==(const PbftVote &other) const
Definition: pbft_vote.cpp:54
const vrf_wrapper::vrf_proof_t & getSortitionProof() const
Get VRF sortition proof.
Definition: pbft_vote.cpp:105
vote_hash_t vote_hash_
Definition: vote.hpp:74
blk_hash_t block_hash_
Definition: vote.hpp:71
sig_t vote_signature_
Definition: vote.hpp:72
const vrf_wrapper::vrf_output_t & getCredential() const
Get VRF sortition output.
Definition: pbft_vote.cpp:115
PbftVote class is a vote class that includes vote hash, vote on PBFT block hash, vote signature,...
Definition: pbft_vote.hpp:19
Vote class is a vote class that includes vote hash, vote on PBFT block hash, vote signature,...
Definition: vote.hpp:15
VrfPbftSortition class used for doing VRF sortition to place a vote or to propose a new PBFT block.
Definition: vrf_sortition.hpp:76
PbftVoteTypes
Definition: vrf_sortition.hpp:21
std::vector<::byte > bytes
Definition: Common.h:46
dev::FixedHash< crypto_vrf_PUBLICKEYBYTES > vrf_pk_t
Definition: vrf_wrapper.hpp:11
Definition: config.hpp:8
uint32_t PbftStep
Definition: types.hpp:26
uint32_t PbftRound
Definition: types.hpp:25
EthBlockNumber PbftPeriod
Definition: types.hpp:24