Kagome
Polkadot Runtime Engine in C++17
vote_tracker_impl.cpp
Go to the documentation of this file.
1 
7 
8 #include "common/visitor.hpp"
10 
12 
14  size_t weight) {
15  auto vote_it = messages_.find(vote.id);
16  if (vote_it == messages_.end()) {
17  messages_[vote.id] = vote;
18  total_weight_ += weight;
19  return PushResult::SUCCESS;
20  }
21 
22  auto &known_vote_variant = vote_it->second;
23 
24  return visit_in_place(
25  known_vote_variant,
26  [&](const SignedMessage &known_vote) {
27  // If it is known vote, it means duplicate
28  if (vote == known_vote) {
30  }
31 
32  // Otherwise, it's another vote of known voter, make it equivocation
33  messages_[vote.id] = EquivocatorySignedMessage(known_vote, vote);
35  },
36  [](const EquivocatorySignedMessage &) {
37  // This is vote of known equivocator
39  });
40  }
41 
42  void VoteTrackerImpl::unpush(const SignedMessage &vote, size_t weight) {
43  auto vote_it = messages_.find(vote.id);
44  if (vote_it == messages_.end()) {
45  return;
46  }
47  auto &existing_vote = vote_it->second;
48  visit_in_place(
49  existing_vote,
50  [&](const SignedMessage &voting_message) {
51  if (voting_message == vote) {
52  messages_.erase(vote_it);
53  total_weight_ -= weight;
54  }
55  },
56  [](const auto &...) {});
57  }
58 
59  std::vector<VoteVariant> VoteTrackerImpl::getMessages() const {
60  std::vector<VoteVariant> votes;
61  // the actual number may be bigger, but it's a good guess
62  votes.reserve(messages_.size());
63  for (const auto &[key, value] : messages_) {
64  votes.push_back(value);
65  }
66  return votes;
67  }
68 
70  return total_weight_;
71  }
72 
73 } // namespace kagome::consensus::grandpa
PushResult push(const SignedMessage &vote, size_t weight) override
std::vector< VoteVariant > getMessages() const override
void unpush(const SignedMessage &vote, size_t weight) override
std::pair< SignedMessage, SignedMessage > EquivocatorySignedMessage
Definition: structs.hpp:94