referrerpolicy=no-referrer-when-downgrade

polkadot_node_network_protocol/request_response/
v1.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Polkadot.
3
4// Polkadot is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Polkadot is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.
16
17//! Requests and responses as sent over the wire for the individual protocols.
18
19use codec::{Decode, Encode};
20
21use polkadot_node_primitives::{
22	AvailableData, DisputeMessage, ErasureChunk, PoV, Proof, UncheckedDisputeMessage,
23};
24use polkadot_primitives::{
25	CandidateHash, CandidateReceiptV2 as CandidateReceipt, Hash, HeadData, Id as ParaId,
26	ValidatorIndex,
27};
28
29use super::{IsRequest, Protocol};
30
31/// Request an availability chunk.
32#[derive(Debug, Copy, Clone, Encode, Decode)]
33pub struct ChunkFetchingRequest {
34	/// Hash of candidate we want a chunk for.
35	pub candidate_hash: CandidateHash,
36	/// The validator index we are requesting from. This must be identical to the index of the
37	/// chunk we'll receive. For v2, this may not be the case.
38	pub index: ValidatorIndex,
39}
40
41/// Receive a requested erasure chunk.
42#[derive(Debug, Clone, Encode, Decode)]
43pub enum ChunkFetchingResponse {
44	/// The requested chunk data.
45	#[codec(index = 0)]
46	Chunk(ChunkResponse),
47	/// Node was not in possession of the requested chunk.
48	#[codec(index = 1)]
49	NoSuchChunk,
50}
51
52impl From<Option<ChunkResponse>> for ChunkFetchingResponse {
53	fn from(x: Option<ChunkResponse>) -> Self {
54		match x {
55			Some(c) => ChunkFetchingResponse::Chunk(c),
56			None => ChunkFetchingResponse::NoSuchChunk,
57		}
58	}
59}
60
61impl From<ChunkFetchingResponse> for Option<ChunkResponse> {
62	fn from(x: ChunkFetchingResponse) -> Self {
63		match x {
64			ChunkFetchingResponse::Chunk(c) => Some(c),
65			ChunkFetchingResponse::NoSuchChunk => None,
66		}
67	}
68}
69
70/// Skimmed down variant of `ErasureChunk`.
71///
72/// Instead of transmitting a full `ErasureChunk` we transmit `ChunkResponse` in
73/// `ChunkFetchingResponse`, which omits the chunk's index. The index is already known by
74/// the requester and by not transmitting it, we ensure the requester is going to use his index
75/// value for validating the response, thus making sure he got what he requested.
76#[derive(Debug, Clone, Encode, Decode)]
77pub struct ChunkResponse {
78	/// The erasure-encoded chunk of data belonging to the candidate block.
79	pub chunk: Vec<u8>,
80	/// Proof for this chunk's branch in the Merkle tree.
81	pub proof: Proof,
82}
83
84impl From<ErasureChunk> for ChunkResponse {
85	fn from(ErasureChunk { chunk, index: _, proof }: ErasureChunk) -> Self {
86		ChunkResponse { chunk, proof }
87	}
88}
89
90impl ChunkResponse {
91	/// Re-build an `ErasureChunk` from response and request.
92	pub fn recombine_into_chunk(self, req: &ChunkFetchingRequest) -> ErasureChunk {
93		ErasureChunk { chunk: self.chunk, proof: self.proof, index: req.index.into() }
94	}
95}
96
97impl IsRequest for ChunkFetchingRequest {
98	type Response = ChunkFetchingResponse;
99	const PROTOCOL: Protocol = Protocol::ChunkFetchingV1;
100}
101
102/// Request the advertised collation at that relay-parent.
103#[derive(Debug, Clone, Encode, Decode)]
104pub struct CollationFetchingRequest {
105	/// Relay parent we want a collation for.
106	pub relay_parent: Hash,
107	/// The `ParaId` of the collation.
108	pub para_id: ParaId,
109}
110
111/// Responses as sent by collators.
112#[derive(Debug, Clone, Encode, Decode)]
113pub enum CollationFetchingResponse {
114	/// Deliver requested collation.
115	#[codec(index = 0)]
116	Collation(CandidateReceipt, PoV),
117
118	/// Deliver requested collation along with parent head data.
119	#[codec(index = 1)]
120	CollationWithParentHeadData {
121		/// The receipt of the candidate.
122		receipt: CandidateReceipt,
123		/// Candidate's proof of validity.
124		pov: PoV,
125		/// The head data of the candidate's parent.
126		/// This is needed for elastic scaling to work.
127		parent_head_data: HeadData,
128	},
129}
130
131impl IsRequest for CollationFetchingRequest {
132	type Response = CollationFetchingResponse;
133	const PROTOCOL: Protocol = Protocol::CollationFetchingV1;
134}
135
136/// Request the advertised collation at that relay-parent.
137#[derive(Debug, Clone, Encode, Decode)]
138pub struct PoVFetchingRequest {
139	/// Candidate we want a PoV for.
140	pub candidate_hash: CandidateHash,
141}
142
143/// Responses to `PoVFetchingRequest`.
144#[derive(Debug, Clone, Encode, Decode)]
145pub enum PoVFetchingResponse {
146	/// Deliver requested PoV.
147	#[codec(index = 0)]
148	PoV(PoV),
149	/// PoV was not found in store.
150	#[codec(index = 1)]
151	NoSuchPoV,
152}
153
154impl IsRequest for PoVFetchingRequest {
155	type Response = PoVFetchingResponse;
156	const PROTOCOL: Protocol = Protocol::PoVFetchingV1;
157}
158
159/// Request the entire available data for a candidate.
160#[derive(Debug, Clone, Encode, Decode)]
161pub struct AvailableDataFetchingRequest {
162	/// The candidate hash to get the available data for.
163	pub candidate_hash: CandidateHash,
164}
165
166/// Receive a requested available data.
167#[derive(Debug, Clone, Encode, Decode)]
168pub enum AvailableDataFetchingResponse {
169	/// The requested data.
170	#[codec(index = 0)]
171	AvailableData(AvailableData),
172	/// Node was not in possession of the requested data.
173	#[codec(index = 1)]
174	NoSuchData,
175}
176
177impl From<Option<AvailableData>> for AvailableDataFetchingResponse {
178	fn from(x: Option<AvailableData>) -> Self {
179		match x {
180			Some(data) => AvailableDataFetchingResponse::AvailableData(data),
181			None => AvailableDataFetchingResponse::NoSuchData,
182		}
183	}
184}
185
186impl IsRequest for AvailableDataFetchingRequest {
187	type Response = AvailableDataFetchingResponse;
188	const PROTOCOL: Protocol = Protocol::AvailableDataFetchingV1;
189}
190
191/// A dispute request.
192///
193/// Contains an invalid vote a valid one for a particular candidate in a given session.
194#[derive(Clone, Encode, Decode, Debug)]
195pub struct DisputeRequest(pub UncheckedDisputeMessage);
196
197impl From<DisputeMessage> for DisputeRequest {
198	fn from(msg: DisputeMessage) -> Self {
199		Self(msg.into())
200	}
201}
202
203/// Possible responses to a `DisputeRequest`.
204#[derive(Encode, Decode, Debug, PartialEq, Eq)]
205pub enum DisputeResponse {
206	/// Recipient successfully processed the dispute request.
207	#[codec(index = 0)]
208	Confirmed,
209}
210
211impl IsRequest for DisputeRequest {
212	type Response = DisputeResponse;
213	const PROTOCOL: Protocol = Protocol::DisputeSendingV1;
214}