soil_network/common/sync/
message.rs1use crate::common::role::Roles;
11
12use bitflags::bitflags;
13use codec::{Decode, Encode, Error, Input, Output};
14pub use generic::{BlockAnnounce, FromBlock};
15use subsoil::runtime::traits::{Block as BlockT, Header as HeaderT, NumberFor};
16
17pub type BlockRequest<B> =
19 generic::BlockRequest<<B as BlockT>::Hash, <<B as BlockT>::Header as HeaderT>::Number>;
20
21pub type BlockData<B> =
23 generic::BlockData<<B as BlockT>::Header, <B as BlockT>::Hash, <B as BlockT>::Extrinsic>;
24
25pub type BlockResponse<B> =
27 generic::BlockResponse<<B as BlockT>::Header, <B as BlockT>::Hash, <B as BlockT>::Extrinsic>;
28
29bitflags! {
31 pub struct BlockAttributes: u8 {
33 const HEADER = 0b00000001;
35 const BODY = 0b00000010;
37 const RECEIPT = 0b00000100;
39 const MESSAGE_QUEUE = 0b00001000;
41 const JUSTIFICATION = 0b00010000;
43 const INDEXED_BODY = 0b00100000;
45 }
46}
47
48impl BlockAttributes {
49 pub fn to_be_u32(&self) -> u32 {
52 u32::from_be_bytes([self.bits(), 0, 0, 0])
53 }
54
55 pub fn from_be_u32(encoded: u32) -> Result<Self, Error> {
57 Self::from_bits(encoded.to_be_bytes()[0])
58 .ok_or_else(|| Error::from("Invalid BlockAttributes"))
59 }
60}
61
62impl Encode for BlockAttributes {
63 fn encode_to<T: Output + ?Sized>(&self, dest: &mut T) {
64 dest.push_byte(self.bits())
65 }
66}
67
68impl codec::EncodeLike for BlockAttributes {}
69
70impl Decode for BlockAttributes {
71 fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
72 Self::from_bits(input.read_byte()?).ok_or_else(|| Error::from("Invalid bytes"))
73 }
74}
75
76#[derive(Debug, PartialEq, Eq, Clone, Copy, Encode, Decode)]
77pub enum Direction {
79 Ascending = 0,
81 Descending = 1,
83}
84
85#[derive(Debug, PartialEq, Eq, Clone, Copy, Encode, Decode)]
87pub enum BlockState {
88 Normal,
90 Best,
92}
93
94#[derive(Debug)]
96pub struct AnnouncementSummary<H: HeaderT> {
97 pub block_hash: H::Hash,
98 pub number: H::Number,
99 pub parent_hash: H::Hash,
100 pub state: Option<BlockState>,
101}
102
103impl<H: HeaderT> BlockAnnounce<H> {
104 pub fn summary(&self) -> AnnouncementSummary<H> {
105 AnnouncementSummary {
106 block_hash: self.header.hash(),
107 number: *self.header.number(),
108 parent_hash: *self.header.parent_hash(),
109 state: self.state,
110 }
111 }
112}
113
114pub mod generic {
116 use super::{BlockAttributes, BlockState, Direction};
117 use crate::common::message::RequestId;
118 use codec::{Decode, Encode, Input, Output};
119 use subsoil::runtime::{EncodedJustification, Justifications};
120
121 #[derive(Debug, PartialEq, Eq, Clone, Encode, Decode)]
123 pub struct BlockData<Header, Hash, Extrinsic> {
124 pub hash: Hash,
126 pub header: Option<Header>,
128 pub body: Option<Vec<Extrinsic>>,
130 pub indexed_body: Option<Vec<Vec<u8>>>,
132 pub receipt: Option<Vec<u8>>,
134 pub message_queue: Option<Vec<u8>>,
136 pub justification: Option<EncodedJustification>,
138 pub justifications: Option<Justifications>,
140 }
141
142 #[derive(Debug, PartialEq, Eq, Clone, Encode, Decode)]
144 pub struct BlockRequest<Hash, Number> {
145 pub id: RequestId,
147 pub fields: BlockAttributes,
149 pub from: FromBlock<Hash, Number>,
151 pub direction: Direction,
153 pub max: Option<u32>,
156 }
157
158 #[derive(Debug, PartialEq, Eq, Clone, Encode, Decode)]
160 pub enum FromBlock<Hash, Number> {
161 Hash(Hash),
163 Number(Number),
165 }
166
167 #[derive(Debug, PartialEq, Eq, Clone, Encode, Decode)]
169 pub struct BlockResponse<Header, Hash, Extrinsic> {
170 pub id: RequestId,
172 pub blocks: Vec<BlockData<Header, Hash, Extrinsic>>,
174 }
175
176 #[derive(Debug, PartialEq, Eq, Clone)]
178 pub struct BlockAnnounce<H> {
179 pub header: H,
181 pub state: Option<BlockState>,
183 pub data: Option<Vec<u8>>,
185 }
186
187 impl<H: Encode> Encode for BlockAnnounce<H> {
191 fn encode_to<T: Output + ?Sized>(&self, dest: &mut T) {
192 self.header.encode_to(dest);
193 if let Some(state) = &self.state {
194 state.encode_to(dest);
195 }
196 if let Some(data) = &self.data {
197 data.encode_to(dest)
198 }
199 }
200 }
201
202 impl<H: Decode> Decode for BlockAnnounce<H> {
203 fn decode<I: Input>(input: &mut I) -> Result<Self, codec::Error> {
204 let header = H::decode(input)?;
205 let state = BlockState::decode(input).ok();
206 let data = Vec::decode(input).ok();
207 Ok(Self { header, state, data })
208 }
209 }
210}
211
212#[derive(Debug, PartialEq, Eq, Clone, Encode, Decode)]
214pub struct BlockAnnouncesHandshake<B: BlockT> {
215 pub roles: Roles,
217 pub best_number: NumberFor<B>,
219 pub best_hash: B::Hash,
221 pub genesis_hash: B::Hash,
223}
224
225impl<B: BlockT> BlockAnnouncesHandshake<B> {
226 pub fn build(
227 roles: Roles,
228 best_number: NumberFor<B>,
229 best_hash: B::Hash,
230 genesis_hash: B::Hash,
231 ) -> Self {
232 Self { genesis_hash, roles, best_number, best_hash }
233 }
234}