Kagome
Polkadot Runtime Engine in C++17
babe_lottery_impl.cpp
Go to the documentation of this file.
1 
7 
8 #include <unordered_set>
9 
10 #include <boost/assert.hpp>
11 #include <scale/scale.hpp>
12 
13 #include "common/buffer.hpp"
14 #include "common/mp_utils.hpp"
16 
17 namespace kagome::consensus {
18  using common::Buffer;
19  namespace vrf_constants = crypto::constants::sr25519::vrf;
20 
22  std::shared_ptr<crypto::VRFProvider> vrf_provider,
23  std::shared_ptr<consensus::babe::BabeConfigRepository> babe_config_repo,
24  std::shared_ptr<crypto::Hasher> hasher)
25  : vrf_provider_{std::move(vrf_provider)},
26  hasher_{std::move(hasher)},
27  logger_{log::createLogger("BabeLottery", "babe_lottery")} {
28  BOOST_ASSERT(vrf_provider_);
29  BOOST_ASSERT(hasher_);
30  BOOST_ASSERT(logger_);
31  BOOST_ASSERT(babe_config_repo);
32  epoch_.epoch_number = std::numeric_limits<uint64_t>::max();
33  }
34 
36  const Randomness &randomness,
37  const Threshold &threshold,
38  const crypto::Sr25519Keypair &keypair) {
39  SL_TRACE(logger_,
40  "Epoch changed "
41  "FROM epoch {} with randomness {} TO epoch {} with randomness {}",
44  epoch.epoch_number,
45  randomness);
46 
47  epoch_ = epoch;
48  randomness_ = randomness;
49  threshold_ = threshold;
50  keypair_ = keypair;
51  }
52 
54  return epoch_;
55  }
56 
57  std::optional<crypto::VRFOutput> BabeLotteryImpl::getSlotLeadership(
58  primitives::BabeSlotNumber slot) const {
59  BOOST_ASSERT_MSG(
60  epoch_.epoch_number != std::numeric_limits<uint64_t>::max(),
61  "Epoch must be initialized before this point");
62 
63  primitives::Transcript transcript;
65 
66  auto res = vrf_provider_->signTranscript(transcript, keypair_, threshold_);
67 
68  SL_TRACE(
69  logger_,
70  "prepareTranscript (leadership): randomness {}, slot {}, epoch {}{}",
72  slot,
74  res.has_value() ? " - SLOT LEADER" : "");
75 
76  return res;
77  }
78 
80  primitives::BabeSlotNumber slot) const {
81  BOOST_ASSERT_MSG(
82  epoch_.epoch_number != std::numeric_limits<uint64_t>::max(),
83  "Epoch must be initialized before this point");
84 
85  primitives::Transcript transcript;
87  auto res = vrf_provider_->signTranscript(transcript, keypair_);
88 
89  BOOST_ASSERT(res);
90  return res.value();
91  }
92 
93  std::optional<primitives::AuthorityIndex>
96  primitives::AuthorityListSize authorities_count,
97  const Randomness &randomness) const {
98  if (0 == authorities_count) {
99  return std::nullopt;
100  }
101 
102  auto rand = hasher_->blake2b_256(
103  scale::encode(std::tuple(randomness, slot)).value());
104 
105  auto rand_number = common::be_bytes_to_uint256(rand);
106 
107  auto index = (rand_number % authorities_count)
108  .convert_to<primitives::AuthorityIndex>();
109 
110  SL_TRACE(logger_,
111  "Secondary author for slot {} has index {}. "
112  "({} authorities. Randomness: {})",
113  slot,
114  index,
115  authorities_count,
116  randomness);
117  return index;
118  }
119 
120 } // namespace kagome::consensus
std::optional< crypto::VRFOutput > getSlotLeadership(primitives::BabeSlotNumber i) const override
std::shared_ptr< crypto::Hasher > hasher_
crypto::VRFOutput slotVrfSignature(primitives::BabeSlotNumber slot) const override
boost::multiprecision::uint256_t be_bytes_to_uint256(gsl::span< const uint8_t, 32 > bytes)
Definition: mp_utils.cpp:111
BabeLotteryImpl(std::shared_ptr< crypto::VRFProvider > vrf_provider, std::shared_ptr< consensus::babe::BabeConfigRepository > babe_config_repo, std::shared_ptr< crypto::Hasher > hasher)
std::optional< primitives::AuthorityIndex > secondarySlotAuthor(primitives::BabeSlotNumber slot, primitives::AuthorityListSize authorities_count, const Randomness &randomness) const override
SLBuffer< std::numeric_limits< size_t >::max()> Buffer
Definition: buffer.hpp:244
uint64_t AuthorityListSize
Definition: authority.hpp:21
void changeEpoch(const EpochDescriptor &epoch, const Randomness &randomness, const Threshold &threshold, const crypto::Sr25519Keypair &keypair) override
std::shared_ptr< crypto::VRFProvider > vrf_provider_
Logger createLogger(const std::string &tag)
Definition: logger.cpp:112
crypto::VRFThreshold Threshold
threshold, which must not be exceeded for the party to be a slot leader
Definition: common.hpp:33
primitives::Transcript & prepareTranscript(primitives::Transcript &transcript, const Randomness &randomness, BabeSlotNumber slot_number, EpochNumber epoch)
EpochDescriptor getEpoch() const override