Kagome
Polkadot Runtime Engine in C++17
vote_crypto_provider_impl.cpp
Go to the documentation of this file.
1 
7 
8 #include "primitives/common.hpp"
9 #include "scale/scale.hpp"
10 
12 
14  const std::shared_ptr<crypto::Ed25519Keypair> &keypair,
15  std::shared_ptr<kagome::crypto::Ed25519Provider> ed_provider,
16  RoundNumber round_number,
17  std::shared_ptr<VoterSet> voter_set)
18  : keypair_{keypair},
19  ed_provider_{std::move(ed_provider)},
20  round_number_{round_number},
21  voter_set_{std::move(voter_set)} {}
22 
23  std::optional<SignedMessage> VoteCryptoProviderImpl::sign(Vote vote) const {
24  if (not keypair_) {
25  return std::nullopt;
26  }
27  auto payload = scale::encode(vote, round_number_, voter_set_->id()).value();
28  auto signature = ed_provider_->sign(*keypair_.get(), payload).value();
29  return {{.message = std::move(vote),
30  .signature = signature,
31  .id = keypair_->public_key}};
32  }
33 
35  RoundNumber number) const {
36  auto payload =
37  scale::encode(vote.message, number, voter_set_->id()).value();
38  auto verifying_result =
39  ed_provider_->verify(vote.signature, payload, vote.id);
40  bool result = verifying_result.has_value() and verifying_result.value();
41 #ifndef NDEBUG // proves really useful for debugging voter set and round number
42  // calculation errors
43  if (!result) {
44  auto logger = log::createLogger("VoteCryptoProvider", "authority");
45  for (auto n = number - 100; n < number + 100; n++) {
46  for (auto id = voter_set_->id() - 100; id < voter_set_->id() + 100; id++) {
47  auto payload = scale::encode(vote.message, n, id).value();
48  auto verifying_result =
49  ed_provider_->verify(vote.signature, payload, vote.id);
50  if (verifying_result.has_value() and verifying_result.value()) {
51  SL_DEBUG(logger,
52  "Correct set id and number are {} {}, actual are {} {}",
53  id,
54  n,
55  voter_set_->id(),
56  number);
57  return false;
58  }
59  }
60  }
61 
62  SL_DEBUG(logger, "Failed to find correct set id");
63  }
64 #endif
65  return result;
66  }
67 
69  const SignedMessage &vote) const {
70  return vote.is<PrimaryPropose>() and verify(vote, round_number_);
71  }
72 
74  return vote.is<Prevote>() and verify(vote, round_number_);
75  }
76 
78  const SignedMessage &vote) const {
79  return vote.is<Precommit>() and verify(vote, round_number_);
80  }
81 
82  std::optional<SignedMessage> VoteCryptoProviderImpl::signPrimaryPropose(
83  const PrimaryPropose &primary_propose) const {
84  return sign(primary_propose);
85  }
86 
87  std::optional<SignedMessage> VoteCryptoProviderImpl::signPrevote(
88  const Prevote &prevote) const {
89  return sign(prevote);
90  }
91 
92  std::optional<SignedMessage> VoteCryptoProviderImpl::signPrecommit(
93  const Precommit &precommit) const {
94  return sign(precommit);
95  }
96 } // namespace kagome::consensus::grandpa
bool verifyPrevote(const SignedMessage &prevote) const override
bool verify(const SignedMessage &vote, RoundNumber number) const
std::optional< SignedMessage > sign(Vote vote) const
boost::variant< Prevote, Precommit, PrimaryPropose > Vote
Note: order of types in variant matters.
Definition: structs.hpp:38
bool verifyPrecommit(const SignedMessage &precommit) const override
std::optional< SignedMessage > signPrimaryPropose(const PrimaryPropose &primary_propose) const override
bool verifyPrimaryPropose(const SignedMessage &primary_propose) const override
std::optional< SignedMessage > signPrevote(const Prevote &prevote) const override
const std::shared_ptr< crypto::Ed25519Keypair > & keypair_
VoteCryptoProviderImpl(const std::shared_ptr< crypto::Ed25519Keypair > &keypair, std::shared_ptr< crypto::Ed25519Provider > ed_provider, RoundNumber round_number, std::shared_ptr< VoterSet > voter_set)
Logger createLogger(const std::string &tag)
Definition: logger.cpp:112
std::shared_ptr< crypto::Ed25519Provider > ed_provider_
std::optional< SignedMessage > signPrecommit(const Precommit &precommit) const override