TARAXA
sortition.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "common/types.hpp"
4 #include "common/vrf_wrapper.hpp"
6 #include "vdf/config.hpp"
7 
9 
10 using namespace vrf_wrapper;
11 
12 // It includes a vrf for difficulty adjustment
14  public:
15  struct InvalidVdfSortition : std::runtime_error {
16  explicit InvalidVdfSortition(std::string const& msg) : runtime_error("Invalid VdfSortition: " + msg) {}
17  };
18 
19  VdfSortition() = default;
20  explicit VdfSortition(SortitionParams const& config, vrf_sk_t const& sk, bytes const& vrf_input, uint64_t vote_count,
21  uint64_t total_vote_count);
22  explicit VdfSortition(bytes const& b);
23  explicit VdfSortition(Json::Value const& json);
24 
25  void computeVdfSolution(const SortitionParams& config, const bytes& msg, const std::atomic_bool& cancelled);
26 
27  void verifyVdf(SortitionParams const& config, bytes const& vrf_input, const vrf_pk_t& pk, bytes const& vdf_input,
28  uint64_t vote_count, uint64_t total_vote_count) const;
29 
30  bytes rlp() const;
31  bool operator==(VdfSortition const& other) const {
32  return vrf_wrapper::VrfSortitionBase::operator==(other) && vdf_sol_.first == other.vdf_sol_.first &&
33  vdf_sol_.second == other.vdf_sol_.second;
34  }
35  bool operator!=(VdfSortition const& other) const { return !operator==(other); }
36 
37  virtual std::ostream& print(std::ostream& strm) const override {
39  strm << " Difficulty: " << getDifficulty() << std::endl;
40  strm << " Computation Time: " << vdf_computation_time_ << std::endl;
41  strm << " Sol1: " << dev::toHex(vdf_sol_.first) << std::endl;
42  strm << " Sol2: " << dev::toHex(vdf_sol_.second) << std::endl;
43  return strm;
44  }
45  friend std::ostream& operator<<(std::ostream& strm, VdfSortition const& vdf) { return vdf.print(strm); }
46 
47  auto getComputationTime() const { return vdf_computation_time_; }
48  uint16_t getDifficulty() const;
49  uint16_t calculateDifficulty(SortitionParams const& config) const;
50  bool isStale(SortitionParams const& config) const;
51  Json::Value getJson() const;
52 
53  private:
54  inline static dev::bytes N = dev::asBytes(
55  "3d1055a514e17cce1290ccb5befb256b00b8aac664e39e754466fcd631004c9e23d16f23"
56  "9aee2a207e5173a7ee8f90ee9ab9b6a745d27c6e850e7ca7332388dfef7e5bbe6267d1f7"
57  "9f9330e44715b3f2066f903081836c1c83ca29126f8fdc5f5922bf3f9ddb4540171691ac"
58  "cc1ef6a34b2a804a18159c89c39b16edee2ede35");
59  bool verifyVrf(const vrf_pk_t& pk, const bytes& vrf_input, uint16_t vote_count) const;
60 
61  std::pair<bytes, bytes> vdf_sol_;
62  unsigned long vdf_computation_time_ = 0;
63  uint16_t difficulty_ = 0;
64  // Votes are normalized to part per thousand of total votes
65  static const uint32_t kVotesProportion = 1000;
66  static const uint32_t kThresholdCorrection = 10;
67 };
68 
69 } // namespace taraxa::vdf_sortition
Definition: sortition.hpp:13
auto getComputationTime() const
Definition: sortition.hpp:47
virtual std::ostream & print(std::ostream &strm) const override
Definition: sortition.hpp:37
friend std::ostream & operator<<(std::ostream &strm, VdfSortition const &vdf)
Definition: sortition.hpp:45
bool operator!=(VdfSortition const &other) const
Definition: sortition.hpp:35
bool operator==(VdfSortition const &other) const
Definition: sortition.hpp:31
std::pair< bytes, bytes > vdf_sol_
Definition: sortition.hpp:61
Definition: vrf_wrapper.hpp:25
virtual std::ostream & print(std::ostream &strm) const
Definition: vrf_wrapper.hpp:43
bool operator==(VrfSortitionBase const &other) const
Definition: vrf_wrapper.hpp:41
bool operator==(std::weak_ptr< NodeEntry > const &_weak, std::shared_ptr< NodeEntry > const &_shared)
Definition: NodeTable.cpp:32
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
std::string toHex(Iterator _it, Iterator _end, std::string const &_prefix)
Definition: CommonData.h:28
bytes asBytes(std::string const &_b)
Converts a string to a byte array containing the string's (byte) data.
Definition: CommonData.h:87
Definition: sortition.hpp:8
vdf_sortition::VdfSortition VdfSortition
Definition: dag_block.hpp:9
Definition: config.hpp:23
InvalidVdfSortition(std::string const &msg)
Definition: sortition.hpp:16