avail_rust_core/
grandpa.rs

1use crate::{AvailHeader, types::AccountId};
2use codec::{Codec, Decode, Encode};
3use primitive_types::H256;
4use serde::{Deserialize, Deserializer, Serialize, Serializer, de};
5
6pub type AuthorityIndex = u64;
7pub type AuthorityWeight = u64;
8pub type AuthorityList = Vec<(AuthorityId, AuthorityWeight)>;
9
10#[derive(Debug, Default, Clone)]
11pub struct AuthorityId(pub [u8; 32]);
12pub type Public = AuthorityId;
13
14impl Encode for AuthorityId {
15	fn encode_to<T: codec::Output + ?Sized>(&self, dest: &mut T) {
16		self.0.encode_to(dest);
17	}
18}
19impl Decode for AuthorityId {
20	fn decode<I: codec::Input>(input: &mut I) -> Result<Self, codec::Error> {
21		Ok(Self(Decode::decode(input)?))
22	}
23}
24impl Serialize for AuthorityId {
25	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
26	where
27		S: Serializer,
28	{
29		let account_id = AccountId::from(self.0);
30		serializer.serialize_str(&account_id.to_string())
31	}
32}
33
34impl<'de> Deserialize<'de> for AuthorityId {
35	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
36	where
37		D: Deserializer<'de>,
38	{
39		let account_id = AccountId::deserialize(deserializer)?;
40		Ok(Self(account_id.0))
41	}
42}
43
44#[derive(Debug, Clone, Serialize, Encode, Decode)]
45pub struct ScheduledChange<N> {
46	/// The new authorities after the change, along with their respective weights.
47	pub next_authorities: AuthorityList,
48	/// The number of blocks to delay.
49	pub delay: N,
50}
51/// An consensus log item for GRANDPA.
52#[derive(Debug, Clone, Serialize, Encode, Decode)]
53#[repr(u8)]
54pub enum ConsensusLog<N: Codec> {
55	ScheduledChange(ScheduledChange<N>) = 1,
56	ForcedChange(N, ScheduledChange<N>) = 2,
57	OnDisabled(AuthorityIndex) = 3,
58	Pause(N) = 4,
59	Resume(N) = 5,
60}
61
62#[derive(Debug, Clone, Copy)]
63pub struct Signature(pub [u8; 64usize]);
64impl Encode for Signature {
65	fn encode_to<T: codec::Output + ?Sized>(&self, dest: &mut T) {
66		self.0.encode_to(dest);
67	}
68}
69impl Decode for Signature {
70	fn decode<I: codec::Input>(input: &mut I) -> Result<Self, codec::Error> {
71		Ok(Self(Decode::decode(input)?))
72	}
73}
74impl Serialize for Signature {
75	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
76	where
77		S: Serializer,
78	{
79		serializer.serialize_str(&const_hex::encode_prefixed(self.0))
80	}
81}
82impl<'de> Deserialize<'de> for Signature {
83	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
84	where
85		D: Deserializer<'de>,
86	{
87		let signature_hex = const_hex::decode(String::deserialize(deserializer)?.trim_start_matches("0x"))
88			.map_err(|e| de::Error::custom(format!("{:?}", e)))?;
89		let signature: [u8; 64usize] = signature_hex
90			.try_into()
91			.map_err(|e| de::Error::custom(format!("{:?}", e)))?;
92		Ok(Self(signature))
93	}
94}
95
96impl Default for Signature {
97	fn default() -> Self {
98		Self([0u8; 64])
99	}
100}
101
102#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
103pub struct Precommit {
104	/// The target block's hash.
105	pub target_hash: H256,
106	/// The target block's number
107	pub target_number: u32,
108}
109impl Encode for Precommit {
110	fn encode_to<T: codec::Output + ?Sized>(&self, dest: &mut T) {
111		self.target_hash.encode_to(dest);
112		self.target_number.encode_to(dest);
113	}
114}
115impl Decode for Precommit {
116	fn decode<I: codec::Input>(input: &mut I) -> Result<Self, codec::Error> {
117		let target_hash = Decode::decode(input)?;
118		let target_number = Decode::decode(input)?;
119		Ok(Self { target_hash, target_number })
120	}
121}
122
123#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
124pub struct SignedPrecommit {
125	/// The precommit message which has been signed.
126	pub precommit: Precommit,
127	/// The signature on the message.
128	pub signature: Signature,
129	/// The Id of the signer.
130	pub id: AuthorityId,
131}
132impl Encode for SignedPrecommit {
133	fn encode_to<T: codec::Output + ?Sized>(&self, dest: &mut T) {
134		self.precommit.encode_to(dest);
135		self.signature.encode_to(dest);
136		self.id.encode_to(dest);
137	}
138}
139impl Decode for SignedPrecommit {
140	fn decode<I: codec::Input>(input: &mut I) -> Result<Self, codec::Error> {
141		let precommit = Decode::decode(input)?;
142		let signature = Decode::decode(input)?;
143		let id = Decode::decode(input)?;
144		Ok(Self { precommit, signature, id })
145	}
146}
147
148#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
149pub struct Commit {
150	/// The target block's hash.
151	pub target_hash: H256,
152	/// The target block's number.
153	pub target_number: u32,
154	/// Precommits for target block or any block after it that justify this commit.
155	pub precommits: Vec<SignedPrecommit>,
156}
157impl Encode for Commit {
158	fn encode_to<T: codec::Output + ?Sized>(&self, dest: &mut T) {
159		self.target_hash.encode_to(dest);
160		self.target_number.encode_to(dest);
161		self.precommits.encode_to(dest);
162	}
163}
164impl Decode for Commit {
165	fn decode<I: codec::Input>(input: &mut I) -> Result<Self, codec::Error> {
166		let target_hash = Decode::decode(input)?;
167		let target_number = Decode::decode(input)?;
168		let precommits = Decode::decode(input)?;
169		Ok(Self { target_hash, target_number, precommits })
170	}
171}
172
173#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
174pub struct GrandpaJustification {
175	pub round: u64,
176	pub commit: Commit,
177	pub votes_ancestries: Vec<AvailHeader>,
178}
179impl Encode for GrandpaJustification {
180	fn encode_to<T: codec::Output + ?Sized>(&self, dest: &mut T) {
181		self.round.encode_to(dest);
182		self.commit.encode_to(dest);
183		self.votes_ancestries.encode_to(dest);
184	}
185}
186impl Decode for GrandpaJustification {
187	fn decode<I: codec::Input>(input: &mut I) -> Result<Self, codec::Error> {
188		let round = Decode::decode(input)?;
189		let commit = Decode::decode(input)?;
190		let votes_ancestries = Decode::decode(input)?;
191		Ok(Self { round, commit, votes_ancestries })
192	}
193}