Kagome
Polkadot Runtime Engine in C++17
structs.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_CONSENSUS_GRANDPA_STRUCTS_
7 #define KAGOME_CONSENSUS_GRANDPA_STRUCTS_
8 
9 #include <boost/asio/steady_timer.hpp>
10 #include <boost/variant.hpp>
11 #include <common/buffer.hpp>
12 
13 #include "common/blob.hpp"
14 #include "common/buffer.hpp"
15 #include "common/visitor.hpp"
16 #include "common/wrapper.hpp"
18 #include "crypto/ed25519_types.hpp"
19 #include "log/logger.hpp"
20 #include "primitives/authority.hpp"
21 #include "primitives/common.hpp"
22 #include "scale/scale.hpp"
23 #include "scale/tie.hpp"
24 
26 
27  using Timer = boost::asio::basic_waitable_timer<std::chrono::steady_clock>;
28 
32  using PrimaryPropose =
34 
36  using Vote = boost::variant<Prevote, // 0
37  Precommit, // 1
39 
40  struct SignedMessage {
43  Id id;
44 
46  return visit_in_place(message,
47  [](const auto &vote) { return vote.number; });
48  }
49 
51  return visit_in_place(message,
52  [](const auto &vote) { return vote.hash; });
53  }
54 
56  return visit_in_place(message, [](const auto &vote) {
57  return BlockInfo(vote.number, vote.hash);
58  });
59  }
60 
61  template <typename T>
62  bool is() const {
63  return message.type() == typeid(T);
64  }
65 
66  bool operator==(const SignedMessage &rhs) const {
67  return message == rhs.message && signature == rhs.signature
68  && id == rhs.id;
69  }
70 
71  bool operator!=(const SignedMessage &rhs) const {
72  return !operator==(rhs);
73  }
74  };
75 
76  template <class Stream,
77  typename = std::enable_if_t<Stream::is_encoder_stream>>
78  Stream &operator<<(Stream &s, const SignedMessage &signed_msg) {
79  return s << signed_msg.message << signed_msg.signature << signed_msg.id;
80  }
81 
82  template <class Stream,
83  typename = std::enable_if_t<Stream::is_decoder_stream>>
84  Stream &operator>>(Stream &s, SignedMessage &signed_msg) {
85  return s >> signed_msg.message >> signed_msg.signature >> signed_msg.id;
86  }
87 
88  template <typename Message>
89  struct Equivocated {
90  Message first;
91  Message second;
92  };
93 
94  using EquivocatorySignedMessage = std::pair<SignedMessage, SignedMessage>;
95  using VoteVariant = boost::variant<SignedMessage, EquivocatorySignedMessage>;
96 
97  namespace detail {
99  template <typename Message>
100  struct Equivocation { // NOLINT
106  };
107  } // namespace detail
108 
109  class SignedPrevote : public SignedMessage {
110  using SignedMessage::SignedMessage;
111  };
112 
113  template <class Stream,
114  typename = std::enable_if_t<Stream::is_encoder_stream>>
115  Stream &operator<<(Stream &s, const SignedPrevote &signed_msg) {
116  assert(signed_msg.template is<Prevote>());
117  return s << boost::strict_get<Prevote>(signed_msg.message)
118  << signed_msg.signature << signed_msg.id;
119  }
120 
121  template <class Stream,
122  typename = std::enable_if_t<Stream::is_decoder_stream>>
123  Stream &operator>>(Stream &s, SignedPrevote &signed_msg) {
124  signed_msg.message = Prevote{};
125  return s >> boost::strict_get<Prevote>(signed_msg.message)
126  >> signed_msg.signature >> signed_msg.id;
127  }
128 
130  using SignedMessage::SignedMessage;
131  };
132 
133  template <class Stream,
134  typename = std::enable_if_t<Stream::is_encoder_stream>>
135  Stream &operator<<(Stream &s, const SignedPrecommit &signed_msg) {
136  assert(signed_msg.template is<Precommit>());
137  return s << boost::strict_get<Precommit>(signed_msg.message)
138  << signed_msg.signature << signed_msg.id;
139  }
140 
141  template <class Stream,
142  typename = std::enable_if_t<Stream::is_decoder_stream>>
144  signed_msg.message = Precommit{};
145  return s >> boost::strict_get<Precommit>(signed_msg.message)
146  >> signed_msg.signature >> signed_msg.id;
147  }
148 
149  // justification that contains a list of signed precommits justifying the
150  // validity of the block
152  SCALE_TIE(3);
153 
156  std::vector<SignedPrecommit> items{};
157  };
158 
160  struct Commit {
163  };
164 
165  // either prevote, precommit or primary propose
166  struct VoteMessage {
167  SCALE_TIE(3);
168 
169  RoundNumber round_number{0};
170  VoterSetId counter{0};
172 
173  Id id() const {
174  return vote.id;
175  }
176  };
177 
180 
181  struct TotalWeight {
182  uint64_t prevote = 0;
183  uint64_t precommit = 0;
184  };
185 
186  // A commit message with compact representation of authentication data.
187  // @See
188  // https://github.com/paritytech/finality-grandpa/blob/v0.14.2/src/lib.rs#L312
189  struct CompactCommit {
190  SCALE_TIE(4);
191 
192  // The target block's hash.
194  // The target block's number.
196  // Precommits for target block or any block after it that justify this
197  // commit.
198  std::vector<Precommit> precommits{};
199  // Authentication data for the commit.
200  std::vector<std::pair<Signature, Id>> auth_data{};
201  };
202 } // namespace kagome::consensus::grandpa
203 
204 #endif // KAGOME_CONSENSUS_GRANDPA_STRUCTS_
boost::asio::basic_waitable_timer< std::chrono::steady_clock > Timer
Definition: structs.hpp:27
A commit message which is an aggregate of precommits.
Definition: structs.hpp:160
boost::variant< SignedMessage, EquivocatorySignedMessage > VoteVariant
Definition: structs.hpp:95
primitives::BlockNumber target_number
Definition: structs.hpp:195
Id id
The identity of the equivocator.
Definition: structs.hpp:104
Proof of an equivocation (double-vote) in a given round.
Definition: structs.hpp:100
boost::variant< Prevote, Precommit, PrimaryPropose > Vote
Note: order of types in variant matters.
Definition: structs.hpp:38
#define SCALE_TIE(N)
Definition: tie.hpp:11
primitives::detail::BlockInfoT< struct PrecommitTag > Precommit
Definition: structs.hpp:30
primitives::BlockInfo BlockInfo
Definition: structs.hpp:29
crypto::Ed25519PublicKey Id
Definition: common.hpp:19
uint32_t BlockNumber
Definition: common.hpp:18
crypto::Ed25519Signature Signature
Definition: common.hpp:22
primitives::BlockNumber BlockNumber
Definition: common.hpp:24
bool operator==(const SignedMessage &rhs) const
Definition: structs.hpp:66
GrandpaJustification justification
Definition: structs.hpp:162
std::pair< SignedMessage, SignedMessage > EquivocatorySignedMessage
Definition: structs.hpp:94
libp2p::connection::Stream Stream
detail::BlockInfoT< struct BlockInfoTag > BlockInfo
Definition: common.hpp:63
primitives::detail::BlockInfoT< struct PrevoteTag > Prevote
Definition: structs.hpp:31
Stream & operator<<(Stream &s, const SignedMessage &signed_msg)
Definition: structs.hpp:78
bool operator!=(const SignedMessage &rhs) const
Definition: structs.hpp:71
Stream & operator>>(Stream &s, SignedMessage &signed_msg)
Definition: structs.hpp:84
RoundNumber round
The round number equivocated in.
Definition: structs.hpp:102