sc_network/protocol/message.rs
1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! Network packet message types. These get serialized and put into the lower level protocol
20//! payload.
21
22use codec::{Decode, Encode};
23use sc_client_api::StorageProof;
24use sc_network_common::message::RequestId;
25
26/// Remote call response.
27#[derive(Debug, PartialEq, Eq, Clone, Encode, Decode)]
28pub struct RemoteCallResponse {
29 /// Id of a request this response was made for.
30 pub id: RequestId,
31 /// Execution proof.
32 pub proof: StorageProof,
33}
34
35#[derive(Debug, PartialEq, Eq, Clone, Encode, Decode)]
36/// Remote read response.
37pub struct RemoteReadResponse {
38 /// Id of a request this response was made for.
39 pub id: RequestId,
40 /// Read proof.
41 pub proof: StorageProof,
42}
43
44/// Generic types.
45pub mod generic {
46 use codec::{Decode, Encode, Input};
47 use sc_client_api::StorageProof;
48 use sc_network_common::{message::RequestId, role::Roles};
49 use sp_runtime::ConsensusEngineId;
50
51 /// Consensus is mostly opaque to us
52 #[derive(Debug, PartialEq, Eq, Clone, Encode, Decode)]
53 pub struct ConsensusMessage {
54 /// Identifies consensus engine.
55 pub protocol: ConsensusEngineId,
56 /// Message payload.
57 pub data: Vec<u8>,
58 }
59
60 /// Status sent on connection.
61 // TODO https://github.com/paritytech/substrate/issues/4674: replace the `Status`
62 // struct with this one, after waiting a few releases beyond `NetworkSpecialization`'s
63 // removal (https://github.com/paritytech/substrate/pull/4665)
64 //
65 // and set MIN_VERSION to 6.
66 #[derive(Debug, PartialEq, Eq, Clone, Encode, Decode)]
67 pub struct CompactStatus<Hash, Number> {
68 /// Protocol version.
69 pub version: u32,
70 /// Minimum supported version.
71 pub min_supported_version: u32,
72 /// Supported roles.
73 pub roles: Roles,
74 /// Best block number.
75 pub best_number: Number,
76 /// Best block hash.
77 pub best_hash: Hash,
78 /// Genesis block hash.
79 pub genesis_hash: Hash,
80 }
81
82 /// Status sent on connection.
83 #[derive(Debug, PartialEq, Eq, Clone, Encode)]
84 pub struct Status<Hash, Number> {
85 /// Protocol version.
86 pub version: u32,
87 /// Minimum supported version.
88 pub min_supported_version: u32,
89 /// Supported roles.
90 pub roles: Roles,
91 /// Best block number.
92 pub best_number: Number,
93 /// Best block hash.
94 pub best_hash: Hash,
95 /// Genesis block hash.
96 pub genesis_hash: Hash,
97 /// DEPRECATED. Chain-specific status.
98 pub chain_status: Vec<u8>,
99 }
100
101 impl<Hash: Decode, Number: Decode> Decode for Status<Hash, Number> {
102 fn decode<I: Input>(value: &mut I) -> Result<Self, codec::Error> {
103 const LAST_CHAIN_STATUS_VERSION: u32 = 5;
104 let compact = CompactStatus::decode(value)?;
105 let chain_status = match <Vec<u8>>::decode(value) {
106 Ok(v) => v,
107 Err(e) =>
108 if compact.version <= LAST_CHAIN_STATUS_VERSION {
109 return Err(e)
110 } else {
111 Vec::new()
112 },
113 };
114
115 let CompactStatus {
116 version,
117 min_supported_version,
118 roles,
119 best_number,
120 best_hash,
121 genesis_hash,
122 } = compact;
123
124 Ok(Self {
125 version,
126 min_supported_version,
127 roles,
128 best_number,
129 best_hash,
130 genesis_hash,
131 chain_status,
132 })
133 }
134 }
135
136 #[derive(Debug, PartialEq, Eq, Clone, Encode, Decode)]
137 /// Remote call request.
138 pub struct RemoteCallRequest<H> {
139 /// Unique request id.
140 pub id: RequestId,
141 /// Block at which to perform call.
142 pub block: H,
143 /// Method name.
144 pub method: String,
145 /// Call data.
146 pub data: Vec<u8>,
147 }
148
149 #[derive(Debug, PartialEq, Eq, Clone, Encode, Decode)]
150 /// Remote storage read request.
151 pub struct RemoteReadRequest<H> {
152 /// Unique request id.
153 pub id: RequestId,
154 /// Block at which to perform call.
155 pub block: H,
156 /// Storage key.
157 pub keys: Vec<Vec<u8>>,
158 }
159
160 #[derive(Debug, PartialEq, Eq, Clone, Encode, Decode)]
161 /// Remote storage read child request.
162 pub struct RemoteReadChildRequest<H> {
163 /// Unique request id.
164 pub id: RequestId,
165 /// Block at which to perform call.
166 pub block: H,
167 /// Child Storage key.
168 pub storage_key: Vec<u8>,
169 /// Storage key.
170 pub keys: Vec<Vec<u8>>,
171 }
172
173 #[derive(Debug, PartialEq, Eq, Clone, Encode, Decode)]
174 /// Remote header request.
175 pub struct RemoteHeaderRequest<N> {
176 /// Unique request id.
177 pub id: RequestId,
178 /// Block number to request header for.
179 pub block: N,
180 }
181
182 #[derive(Debug, PartialEq, Eq, Clone, Encode, Decode)]
183 /// Remote header response.
184 pub struct RemoteHeaderResponse<Header> {
185 /// Id of a request this response was made for.
186 pub id: RequestId,
187 /// Header. None if proof generation has failed (e.g. header is unknown).
188 pub header: Option<Header>,
189 /// Header proof.
190 pub proof: StorageProof,
191 }
192
193 #[derive(Debug, PartialEq, Eq, Clone, Encode, Decode)]
194 /// Remote changes request.
195 pub struct RemoteChangesRequest<H> {
196 /// Unique request id.
197 pub id: RequestId,
198 /// Hash of the first block of the range (including first) where changes are requested.
199 pub first: H,
200 /// Hash of the last block of the range (including last) where changes are requested.
201 pub last: H,
202 /// Hash of the first block for which the requester has the changes trie root. All other
203 /// affected roots must be proved.
204 pub min: H,
205 /// Hash of the last block that we can use when querying changes.
206 pub max: H,
207 /// Storage child node key which changes are requested.
208 pub storage_key: Option<Vec<u8>>,
209 /// Storage key which changes are requested.
210 pub key: Vec<u8>,
211 }
212
213 #[derive(Debug, PartialEq, Eq, Clone, Encode, Decode)]
214 /// Remote changes response.
215 pub struct RemoteChangesResponse<N, H> {
216 /// Id of a request this response was made for.
217 pub id: RequestId,
218 /// Proof has been generated using block with this number as a max block. Should be
219 /// less than or equal to the RemoteChangesRequest::max block number.
220 pub max: N,
221 /// Changes proof.
222 pub proof: Vec<Vec<u8>>,
223 /// Changes tries roots missing on the requester' node.
224 pub roots: Vec<(N, H)>,
225 /// Missing changes tries roots proof.
226 pub roots_proof: StorageProof,
227 }
228}