1use crate::role::Roles;
23
24use bitflags::bitflags;
25use codec::{Decode, Encode, Error, Input, Output};
26pub use generic::{BlockAnnounce, FromBlock};
27use sp_runtime::traits::{Block as BlockT, Header as HeaderT, NumberFor};
28
29pub type BlockRequest<B> =
31 generic::BlockRequest<<B as BlockT>::Hash, <<B as BlockT>::Header as HeaderT>::Number>;
32
33pub type BlockData<B> =
35 generic::BlockData<<B as BlockT>::Header, <B as BlockT>::Hash, <B as BlockT>::Extrinsic>;
36
37pub type BlockResponse<B> =
39 generic::BlockResponse<<B as BlockT>::Header, <B as BlockT>::Hash, <B as BlockT>::Extrinsic>;
40
41bitflags! {
43 pub struct BlockAttributes: u8 {
45 const HEADER = 0b00000001;
47 const BODY = 0b00000010;
49 const RECEIPT = 0b00000100;
51 const MESSAGE_QUEUE = 0b00001000;
53 const JUSTIFICATION = 0b00010000;
55 const DEPRECATED_INDEXED_BODY = 0b00100000;
58 }
59}
60
61impl BlockAttributes {
62 pub fn to_be_u32(&self) -> u32 {
65 u32::from_be_bytes([self.bits(), 0, 0, 0])
66 }
67
68 pub fn from_be_u32(encoded: u32) -> Result<Self, Error> {
70 Self::from_bits(encoded.to_be_bytes()[0])
71 .ok_or_else(|| Error::from("Invalid BlockAttributes"))
72 }
73}
74
75impl Encode for BlockAttributes {
76 fn encode_to<T: Output + ?Sized>(&self, dest: &mut T) {
77 dest.push_byte(self.bits())
78 }
79}
80
81impl codec::EncodeLike for BlockAttributes {}
82
83impl Decode for BlockAttributes {
84 fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
85 Self::from_bits(input.read_byte()?).ok_or_else(|| Error::from("Invalid bytes"))
86 }
87}
88
89#[derive(Debug, PartialEq, Eq, Clone, Copy, Encode, Decode)]
90pub enum Direction {
92 Ascending = 0,
94 Descending = 1,
96}
97
98#[derive(Debug, PartialEq, Eq, Clone, Copy, Encode, Decode)]
100pub enum BlockState {
101 Normal,
103 Best,
105}
106
107#[derive(Debug)]
109pub struct AnnouncementSummary<H: HeaderT> {
110 pub block_hash: H::Hash,
111 pub number: H::Number,
112 pub parent_hash: H::Hash,
113 pub state: Option<BlockState>,
114}
115
116impl<H: HeaderT> BlockAnnounce<H> {
117 pub fn summary(&self) -> AnnouncementSummary<H> {
118 AnnouncementSummary {
119 block_hash: self.header.hash(),
120 number: *self.header.number(),
121 parent_hash: *self.header.parent_hash(),
122 state: self.state,
123 }
124 }
125}
126
127pub mod generic {
129 use super::{BlockAttributes, BlockState, Direction};
130 use crate::message::RequestId;
131 use codec::{Decode, Encode, Input, Output};
132 use sp_runtime::{EncodedJustification, Justifications};
133
134 #[derive(Debug, PartialEq, Eq, Clone, Encode, Decode)]
136 pub struct BlockData<Header, Hash, Extrinsic> {
137 pub hash: Hash,
139 pub header: Option<Header>,
141 pub body: Option<Vec<Extrinsic>>,
143 pub receipt: Option<Vec<u8>>,
145 pub message_queue: Option<Vec<u8>>,
147 pub justification: Option<EncodedJustification>,
149 pub justifications: Option<Justifications>,
151 }
152
153 #[derive(Debug, PartialEq, Eq, Clone, Encode, Decode)]
155 pub struct BlockRequest<Hash, Number> {
156 pub id: RequestId,
158 pub fields: BlockAttributes,
160 pub from: FromBlock<Hash, Number>,
162 pub direction: Direction,
164 pub max: Option<u32>,
167 }
168
169 #[derive(Debug, PartialEq, Eq, Clone, Encode, Decode)]
171 pub enum FromBlock<Hash, Number> {
172 Hash(Hash),
174 Number(Number),
176 }
177
178 #[derive(Debug, PartialEq, Eq, Clone, Encode, Decode)]
180 pub struct BlockResponse<Header, Hash, Extrinsic> {
181 pub id: RequestId,
183 pub blocks: Vec<BlockData<Header, Hash, Extrinsic>>,
185 }
186
187 #[derive(Debug, PartialEq, Eq, Clone)]
189 pub struct BlockAnnounce<H> {
190 pub header: H,
192 pub state: Option<BlockState>,
194 pub data: Option<Vec<u8>>,
196 }
197
198 impl<H: Encode> Encode for BlockAnnounce<H> {
202 fn encode_to<T: Output + ?Sized>(&self, dest: &mut T) {
203 self.header.encode_to(dest);
204 if let Some(state) = &self.state {
205 state.encode_to(dest);
206 }
207 if let Some(data) = &self.data {
208 data.encode_to(dest)
209 }
210 }
211 }
212
213 impl<H: Decode> Decode for BlockAnnounce<H> {
214 fn decode<I: Input>(input: &mut I) -> Result<Self, codec::Error> {
215 let header = H::decode(input)?;
216 let state = BlockState::decode(input).ok();
217 let data = Vec::decode(input).ok();
218 Ok(Self { header, state, data })
219 }
220 }
221}
222
223#[derive(Debug, PartialEq, Eq, Clone, Encode, Decode)]
225pub struct BlockAnnouncesHandshake<B: BlockT> {
226 pub roles: Roles,
228 pub best_number: NumberFor<B>,
230 pub best_hash: B::Hash,
232 pub genesis_hash: B::Hash,
234}
235
236impl<B: BlockT> BlockAnnouncesHandshake<B> {
237 pub fn build(
238 roles: Roles,
239 best_number: NumberFor<B>,
240 best_hash: B::Hash,
241 genesis_hash: B::Hash,
242 ) -> Self {
243 Self { genesis_hash, roles, best_number, best_hash }
244 }
245}