TARAXA
vrf_sortition.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <libdevcore/SHA3.h>
3 #include <libdevcrypto/Common.h>
4 
5 #include <boost/multiprecision/mpfr.hpp>
6 
7 #include "common/types.hpp"
8 #include "common/vrf_wrapper.hpp"
9 
10 namespace taraxa {
11 
16 // PbftVoteTypes are equal to step numbers, e.g.
17 // - "propose_vote" type can be created only in step 1
18 // - "soft_vote" type can be created only in step 2
19 // - "cert_vote" type can be created only in step 3
20 // - "next_vote" type can be created only in step 4 or more
22 
26 class VrfPbftMsg {
27  public:
28  VrfPbftMsg() = default;
29  VrfPbftMsg(PbftVoteTypes type, PbftPeriod period, PbftRound round, PbftStep step);
30 
36  PbftVoteTypes getType() const;
37 
42  std::string toString() const;
43 
48  bytes getRlpBytes() const;
49 
50  bool operator==(VrfPbftMsg const& other) const;
51  friend std::ostream& operator<<(std::ostream& strm, VrfPbftMsg const& pbft_msg) {
52  strm << " [Vrf Pbft Msg] " << std::endl;
53  strm << " period: " << pbft_msg.period_ << std::endl;
54  strm << " round: " << pbft_msg.round_ << std::endl;
55  strm << " step: " << pbft_msg.step_ << std::endl;
56  return strm;
57  }
58 
62 };
63 
71 dev::h256 getVoterIndexHash(const vrf_wrapper::vrf_output_t& vrf, const public_t& address, uint64_t index = 0);
72 
81 
82  public:
83  VrfPbftSortition() = default;
84 
85  VrfPbftSortition(vrf_sk_t const& sk, const VrfPbftMsg& pbft_msg)
86  : VrfSortitionBase(sk, pbft_msg.getRlpBytes()), pbft_msg_(pbft_msg) {}
87 
88  explicit VrfPbftSortition(dev::bytes const& rlp);
89 
94  dev::bytes getRlpBytes() const;
95 
102  bool verify(const vrf_pk_t& pk, bool strict = true) const {
103  return VrfSortitionBase::verify(pk, pbft_msg_.getRlpBytes(), 1, strict);
104  }
105 
106  bool operator==(VrfPbftSortition const& other) const {
108  }
109 
110  static inline uint256_t max256bits = std::numeric_limits<uint256_t>::max();
111  static inline auto kMax256bFP = max256bits.convert_to<boost::multiprecision::mpfr_float>();
112 
121  static uint64_t getBinominalDistribution(uint64_t stake, double dpos_total_votes_count, double threshold,
122  const uint256_t& hash);
123 
132  uint64_t calculateWeight(uint64_t stake, uint64_t dpos_total_votes_count, uint64_t threshold,
133  const public_t& address) const;
134 
135  friend std::ostream& operator<<(std::ostream& strm, const VrfPbftSortition& vrf_sortition) {
136  strm << "[VRF sortition] " << std::endl;
137  strm << " proof: " << vrf_sortition.proof_ << std::endl;
138  strm << " output: " << vrf_sortition.output_ << std::endl;
139  strm << vrf_sortition.pbft_msg_ << std::endl;
140  return strm;
141  }
142 
144 };
145 
148 } // namespace taraxa
Definition: vrf_wrapper.hpp:25
vrf_output_t output_
Definition: vrf_wrapper.hpp:74
bool operator==(VrfSortitionBase const &other) const
Definition: vrf_wrapper.hpp:41
vrf_proof_t proof_
Definition: vrf_wrapper.hpp:73
bool verify(const vrf_pk_t &pk, const bytes &msg, uint16_t vote_count=1, bool strict=true) const
Definition: vrf_wrapper.cpp:55
bool verify(const vrf_pk_t &pk, bool strict=true) const
Verify VRF sortition.
Definition: vrf_sortition.hpp:102
PbftPeriod period_
Definition: vrf_sortition.hpp:59
VrfPbftMsg pbft_msg_
Definition: vrf_sortition.hpp:143
VrfPbftSortition(vrf_sk_t const &sk, const VrfPbftMsg &pbft_msg)
Definition: vrf_sortition.hpp:85
PbftRound round_
Definition: vrf_sortition.hpp:60
bytes getRlpBytes() const
Get bytes of RLP stream.
Definition: vrf_sortition.cpp:41
friend std::ostream & operator<<(std::ostream &strm, const VrfPbftSortition &vrf_sortition)
Definition: vrf_sortition.hpp:135
PbftVoteTypes getType() const
Get v type based on vote step.
Definition: vrf_sortition.cpp:17
std::string toString() const
Combine vote type, PBFT period, round and step to a string.
Definition: vrf_sortition.cpp:33
PbftStep step_
Definition: vrf_sortition.hpp:61
bool operator==(VrfPbftMsg const &other) const
Definition: vrf_sortition.cpp:37
friend std::ostream & operator<<(std::ostream &strm, VrfPbftMsg const &pbft_msg)
Definition: vrf_sortition.hpp:51
uint64_t calculateWeight(uint64_t stake, uint64_t dpos_total_votes_count, uint64_t threshold, const public_t &address) const
Calculate vote weight.
Definition: vrf_sortition.cpp:85
dev::bytes getRlpBytes() const
Get bytes of RLP stream.
Definition: vrf_sortition.cpp:55
VrfPbftMsg()=default
static uint256_t max256bits
Definition: vrf_sortition.hpp:110
static uint64_t getBinominalDistribution(uint64_t stake, double dpos_total_votes_count, double threshold, const uint256_t &hash)
Calculate a vote weight in binominal distribution.
Definition: vrf_sortition.cpp:67
static auto kMax256bFP
Definition: vrf_sortition.hpp:111
bool operator==(VrfPbftSortition const &other) const
Definition: vrf_sortition.hpp:106
VrfPbftMsg class uses PBFT period, round and step to generate a message for doing VRF sortition.
Definition: vrf_sortition.hpp:26
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
dev::h256 getVoterIndexHash(const vrf_wrapper::vrf_output_t &vrf, const public_t &address, uint64_t index=0)
Get a hash number of combining VRF output, voter address, and vote weight index.
Definition: vrf_sortition.cpp:93
std::vector<::byte > bytes
Definition: Common.h:46
bytes rlp(_T _t)
Export a single item in RLP format, returning a byte array.
Definition: RLP.h:665
dev::FixedHash< crypto_vrf_PUBLICKEYBYTES > vrf_pk_t
Definition: vrf_wrapper.hpp:11
dev::FixedHash< crypto_vrf_PROOFBYTES > vrf_proof_t
Definition: vrf_wrapper.hpp:13
dev::FixedHash< crypto_vrf_SECRETKEYBYTES > vrf_sk_t
Definition: vrf_wrapper.hpp:12
dev::FixedHash< crypto_vrf_OUTPUTBYTES > vrf_output_t
Definition: vrf_wrapper.hpp:14
Definition: config.hpp:8
uint32_t PbftStep
Definition: types.hpp:26
uint32_t PbftRound
Definition: types.hpp:25
EthBlockNumber PbftPeriod
Definition: types.hpp:24
boost::multiprecision::uint256_t uint256_t
Definition: types.hpp:29