referrerpolicy=no-referrer-when-downgrade
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.

//! A generic av store subsystem mockup suitable to be used in benchmarks.

use crate::network::{HandleNetworkMessage, NetworkMessage};
use codec::Encode;
use futures::{channel::oneshot, FutureExt};
use polkadot_node_network_protocol::request_response::{
	v1::AvailableDataFetchingResponse, v2::ChunkFetchingResponse, Protocol, ReqProtocolNames,
	Requests,
};
use polkadot_node_primitives::{AvailableData, ErasureChunk};
use polkadot_node_subsystem::{
	messages::AvailabilityStoreMessage, overseer, SpawnedSubsystem, SubsystemError,
};
use polkadot_node_subsystem_types::OverseerSignal;
use polkadot_primitives::{CandidateHash, ChunkIndex, CoreIndex, ValidatorIndex};
use std::collections::HashMap;

pub struct AvailabilityStoreState {
	candidate_hashes: HashMap<CandidateHash, usize>,
	chunks: Vec<Vec<ErasureChunk>>,
	chunk_indices: Vec<Vec<ChunkIndex>>,
	candidate_hash_to_core_index: HashMap<CandidateHash, CoreIndex>,
}

const LOG_TARGET: &str = "subsystem-bench::av-store-mock";

/// Mockup helper. Contains Chunks and full availability data of all parachain blocks
/// used in a test.
#[derive(Clone)]
pub struct NetworkAvailabilityState {
	pub req_protocol_names: ReqProtocolNames,
	pub candidate_hashes: HashMap<CandidateHash, usize>,
	pub available_data: Vec<AvailableData>,
	pub chunks: Vec<Vec<ErasureChunk>>,
	pub chunk_indices: Vec<Vec<ChunkIndex>>,
	pub candidate_hash_to_core_index: HashMap<CandidateHash, CoreIndex>,
}

// Implement access to the state.
#[async_trait::async_trait]
impl HandleNetworkMessage for NetworkAvailabilityState {
	async fn handle(
		&self,
		message: NetworkMessage,
		_node_sender: &mut futures::channel::mpsc::UnboundedSender<NetworkMessage>,
	) -> Option<NetworkMessage> {
		match message {
			NetworkMessage::RequestFromNode(peer, request) => match request {
				Requests::ChunkFetching(outgoing_request) => {
					gum::debug!(target: LOG_TARGET, request = ?outgoing_request, "Received `RequestFromNode`");
					let validator_index: usize = outgoing_request.payload.index.0 as usize;
					let candidate_hash = outgoing_request.payload.candidate_hash;

					let candidate_index = self
						.candidate_hashes
						.get(&candidate_hash)
						.expect("candidate was generated previously; qed");
					gum::warn!(target: LOG_TARGET, ?candidate_hash, candidate_index, "Candidate mapped to index");

					let candidate_chunks = self.chunks.get(*candidate_index).unwrap();
					let chunk_indices = self
						.chunk_indices
						.get(
							self.candidate_hash_to_core_index.get(&candidate_hash).unwrap().0
								as usize,
						)
						.unwrap();

					let chunk = candidate_chunks
						.get(chunk_indices.get(validator_index).unwrap().0 as usize)
						.unwrap();

					let response = Ok((
						ChunkFetchingResponse::from(Some(chunk.clone())).encode(),
						self.req_protocol_names.get_name(Protocol::ChunkFetchingV2),
					));

					if let Err(err) = outgoing_request.pending_response.send(response) {
						gum::error!(target: LOG_TARGET, ?err, "Failed to send `ChunkFetchingResponse`");
					}

					None
				},
				Requests::AvailableDataFetchingV1(outgoing_request) => {
					let candidate_hash = outgoing_request.payload.candidate_hash;
					let candidate_index = self
						.candidate_hashes
						.get(&candidate_hash)
						.expect("candidate was generated previously; qed");
					gum::debug!(target: LOG_TARGET, ?candidate_hash, candidate_index, "Candidate mapped to index");

					let available_data = self.available_data.get(*candidate_index).unwrap().clone();

					let response = Ok((
						AvailableDataFetchingResponse::from(Some(available_data)).encode(),
						self.req_protocol_names.get_name(Protocol::AvailableDataFetchingV1),
					));
					outgoing_request
						.pending_response
						.send(response)
						.expect("Response is always sent successfully");
					None
				},
				_ => Some(NetworkMessage::RequestFromNode(peer, request)),
			},

			message => Some(message),
		}
	}
}

/// A mock of the availability store subsystem. This one also generates all the
/// candidates that a
pub struct MockAvailabilityStore {
	state: AvailabilityStoreState,
}

impl MockAvailabilityStore {
	pub fn new(
		chunks: Vec<Vec<ErasureChunk>>,
		chunk_indices: Vec<Vec<ChunkIndex>>,
		candidate_hashes: HashMap<CandidateHash, usize>,
		candidate_hash_to_core_index: HashMap<CandidateHash, CoreIndex>,
	) -> MockAvailabilityStore {
		Self {
			state: AvailabilityStoreState {
				chunks,
				candidate_hashes,
				chunk_indices,
				candidate_hash_to_core_index,
			},
		}
	}

	async fn respond_to_query_all_request(
		&self,
		candidate_hash: CandidateHash,
		send_chunk: impl Fn(ValidatorIndex) -> bool,
		tx: oneshot::Sender<Vec<(ValidatorIndex, ErasureChunk)>>,
	) {
		let candidate_index = self
			.state
			.candidate_hashes
			.get(&candidate_hash)
			.expect("candidate was generated previously; qed");
		gum::debug!(target: LOG_TARGET, ?candidate_hash, candidate_index, "Candidate mapped to index");

		let n_validators = self.state.chunks[0].len();
		let candidate_chunks = self.state.chunks.get(*candidate_index).unwrap();
		let core_index = self.state.candidate_hash_to_core_index.get(&candidate_hash).unwrap();
		// We'll likely only send our chunk, so use capacity 1.
		let mut v = Vec::with_capacity(1);

		for validator_index in 0..n_validators {
			if !send_chunk(ValidatorIndex(validator_index as u32)) {
				continue;
			}
			let chunk_index = self
				.state
				.chunk_indices
				.get(core_index.0 as usize)
				.unwrap()
				.get(validator_index)
				.unwrap();

			let chunk = candidate_chunks.get(chunk_index.0 as usize).unwrap().clone();
			v.push((ValidatorIndex(validator_index as u32), chunk.clone()));
		}

		let _ = tx.send(v);
	}
}

#[overseer::subsystem(AvailabilityStore, error=SubsystemError, prefix=self::overseer)]
impl<Context> MockAvailabilityStore {
	fn start(self, ctx: Context) -> SpawnedSubsystem {
		let future = self.run(ctx).map(|_| Ok(())).boxed();

		SpawnedSubsystem { name: "test-environment", future }
	}
}

#[overseer::contextbounds(AvailabilityStore, prefix = self::overseer)]
impl MockAvailabilityStore {
	async fn run<Context>(self, mut ctx: Context) {
		gum::debug!(target: LOG_TARGET, "Subsystem running");
		loop {
			let msg = ctx.recv().await.expect("Overseer never fails us");

			match msg {
				orchestra::FromOrchestra::Signal(signal) =>
					if signal == OverseerSignal::Conclude {
						return
					},
				orchestra::FromOrchestra::Communication { msg } => match msg {
					AvailabilityStoreMessage::QueryAvailableData(candidate_hash, tx) => {
						gum::debug!(target: LOG_TARGET, candidate_hash = ?candidate_hash, "Responding to QueryAvailableData");

						// We never have the full available data.
						let _ = tx.send(None);
					},
					AvailabilityStoreMessage::QueryAllChunks(candidate_hash, tx) => {
						// We always have our own chunk.
						gum::debug!(target: LOG_TARGET, candidate_hash = ?candidate_hash, "Responding to QueryAllChunks");
						self.respond_to_query_all_request(
							candidate_hash,
							|index| index == 0.into(),
							tx,
						)
						.await;
					},
					AvailabilityStoreMessage::QueryChunkSize(candidate_hash, tx) => {
						gum::debug!(target: LOG_TARGET, candidate_hash = ?candidate_hash, "Responding to QueryChunkSize");

						let candidate_index = self
							.state
							.candidate_hashes
							.get(&candidate_hash)
							.expect("candidate was generated previously; qed");
						gum::debug!(target: LOG_TARGET, ?candidate_hash, candidate_index, "Candidate mapped to index");

						let chunk_size = self
							.state
							.chunks
							.get(*candidate_index)
							.unwrap()
							.first()
							.unwrap()
							.encoded_size();
						let _ = tx.send(Some(chunk_size));
					},
					AvailabilityStoreMessage::StoreChunk {
						candidate_hash,
						chunk,
						tx,
						validator_index,
					} => {
						gum::debug!(
							target: LOG_TARGET,
							chunk_index = ?chunk.index,
							validator_index = ?validator_index,
							candidate_hash = ?candidate_hash,
							"Responding to StoreChunk"
						);
						let _ = tx.send(Ok(()));
					},
					_ => {
						unimplemented!("Unexpected av-store message")
					},
				},
			}
		}
	}
}