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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// 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 runtime api subsystem mockup suitable to be used in benchmarks.

use crate::configuration::{TestAuthorities, TestConfiguration};
use bitvec::prelude::BitVec;
use futures::FutureExt;
use itertools::Itertools;
use polkadot_node_subsystem::{
	messages::{RuntimeApiMessage, RuntimeApiRequest},
	overseer, SpawnedSubsystem, SubsystemError,
};
use polkadot_node_subsystem_types::OverseerSignal;
use polkadot_primitives::{
	node_features,
	vstaging::{CandidateEvent, CandidateReceiptV2 as CandidateReceipt, CoreState, OccupiedCore},
	ApprovalVotingParams, AsyncBackingParams, CoreIndex, GroupIndex, GroupRotationInfo,
	Id as ParaId, IndexedVec, NodeFeatures, ScheduledCore, SessionIndex, SessionInfo,
	ValidationCode, ValidatorIndex,
};
use sp_consensus_babe::Epoch as BabeEpoch;
use sp_core::H256;
use std::collections::{BTreeMap, HashMap, VecDeque};

const LOG_TARGET: &str = "subsystem-bench::runtime-api-mock";

/// Minimal state to answer requests.
#[derive(Clone)]
pub struct RuntimeApiState {
	// All authorities in the test,
	authorities: TestAuthorities,
	// Node features state in the runtime
	node_features: NodeFeatures,
	// Candidate hashes per block
	candidate_hashes: HashMap<H256, Vec<CandidateReceipt>>,
	// Included candidates per bock
	included_candidates: HashMap<H256, Vec<CandidateEvent>>,
	babe_epoch: Option<BabeEpoch>,
	// The session child index,
	session_index: SessionIndex,
	// The claim queue
	claim_queue: BTreeMap<CoreIndex, VecDeque<ParaId>>,
}

#[derive(Clone)]
pub enum MockRuntimeApiCoreState {
	Occupied,
	Scheduled,
	#[allow(dead_code)]
	Free,
}

/// A mocked `runtime-api` subsystem.
#[derive(Clone)]
pub struct MockRuntimeApi {
	state: RuntimeApiState,
	config: TestConfiguration,
	core_state: MockRuntimeApiCoreState,
}

impl MockRuntimeApi {
	pub fn new(
		config: TestConfiguration,
		authorities: TestAuthorities,
		candidate_hashes: HashMap<H256, Vec<CandidateReceipt>>,
		included_candidates: HashMap<H256, Vec<CandidateEvent>>,
		babe_epoch: Option<BabeEpoch>,
		session_index: SessionIndex,
		core_state: MockRuntimeApiCoreState,
	) -> MockRuntimeApi {
		// Enable chunk mapping feature to make systematic av-recovery possible.
		let node_features = default_node_features();
		let validator_group_count =
			session_info_for_peers(&config, &authorities).validator_groups.len();

		// Each para gets one core assigned and there is only one candidate per
		// parachain per relay chain block (no elastic scaling).
		let claim_queue = candidate_hashes
			.iter()
			.next()
			.expect("Candidates are generated at test start")
			.1
			.iter()
			.enumerate()
			.map(|(index, candidate_receipt)| {
				// Ensure test breaks if badly configured.
				assert!(index < validator_group_count);
				(CoreIndex(index as u32), vec![candidate_receipt.descriptor.para_id()].into())
			})
			.collect();

		Self {
			state: RuntimeApiState {
				authorities,
				candidate_hashes,
				included_candidates,
				babe_epoch,
				session_index,
				node_features,
				claim_queue,
			},
			config,
			core_state,
		}
	}

	fn session_info(&self) -> SessionInfo {
		session_info_for_peers(&self.config, &self.state.authorities)
	}
}

/// Generates a test session info with all passed authorities as consensus validators.
pub fn session_info_for_peers(
	configuration: &TestConfiguration,
	authorities: &TestAuthorities,
) -> SessionInfo {
	let all_validators = (0..configuration.n_validators)
		.map(|i| ValidatorIndex(i as _))
		.collect::<Vec<_>>();

	let validator_groups = all_validators
		.chunks(configuration.max_validators_per_core)
		.map(Vec::from)
		.collect::<Vec<_>>();

	SessionInfo {
		validators: authorities.validator_public.iter().cloned().collect(),
		discovery_keys: authorities.validator_authority_id.to_vec(),
		assignment_keys: authorities.validator_assignment_id.to_vec(),
		validator_groups: IndexedVec::<GroupIndex, Vec<ValidatorIndex>>::from(validator_groups),
		n_cores: configuration.n_cores as u32,
		needed_approvals: configuration.needed_approvals as u32,
		zeroth_delay_tranche_width: configuration.zeroth_delay_tranche_width as u32,
		relay_vrf_modulo_samples: configuration.relay_vrf_modulo_samples as u32,
		n_delay_tranches: configuration.n_delay_tranches as u32,
		no_show_slots: configuration.no_show_slots as u32,
		active_validator_indices: (0..authorities.validator_authority_id.len())
			.map(|index| ValidatorIndex(index as u32))
			.collect_vec(),
		dispute_period: 6,
		random_seed: [0u8; 32],
	}
}

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

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

#[overseer::contextbounds(RuntimeApi, prefix = self::overseer)]
impl MockRuntimeApi {
	async fn run<Context>(self, mut ctx: Context) {
		let validator_group_count = self.session_info().validator_groups.len();

		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 } => {
					gum::debug!(target: LOG_TARGET, msg=?msg, "recv message");

					match msg {
						RuntimeApiMessage::Request(
							request,
							RuntimeApiRequest::CandidateEvents(sender),
						) => {
							let candidate_events = self.state.included_candidates.get(&request);
							let _ = sender.send(Ok(candidate_events.cloned().unwrap_or_default()));
						},
						RuntimeApiMessage::Request(
							_block_hash,
							RuntimeApiRequest::SessionInfo(_session_index, sender),
						) => {
							let _ = sender.send(Ok(Some(self.session_info())));
						},
						RuntimeApiMessage::Request(
							_block_hash,
							RuntimeApiRequest::NodeFeatures(_session_index, sender),
						) => {
							let _ = sender.send(Ok(self.state.node_features.clone()));
						},
						RuntimeApiMessage::Request(
							_block_hash,
							RuntimeApiRequest::SessionExecutorParams(_session_index, sender),
						) => {
							let _ = sender.send(Ok(Some(Default::default())));
						},
						RuntimeApiMessage::Request(
							_block_hash,
							RuntimeApiRequest::Validators(sender),
						) => {
							let _ =
								sender.send(Ok(self.state.authorities.validator_public.clone()));
						},
						RuntimeApiMessage::Request(
							_block_hash,
							RuntimeApiRequest::SessionIndexForChild(sender),
						) => {
							// Session is always the same.
							let _ = sender.send(Ok(self.state.session_index));
						},
						RuntimeApiMessage::Request(
							block_hash,
							RuntimeApiRequest::AvailabilityCores(sender),
						) => {
							let candidate_hashes = self
								.state
								.candidate_hashes
								.get(&block_hash)
								.expect("Relay chain block hashes are generated at test start");

							// All cores are always occupied.
							let cores = candidate_hashes
								.iter()
								.enumerate()
								.map(|(index, candidate_receipt)| {
									// Ensure test breaks if badly configured.
									assert!(index < validator_group_count);

									use MockRuntimeApiCoreState::*;
									match self.core_state {
										Occupied => CoreState::Occupied(OccupiedCore {
											next_up_on_available: None,
											occupied_since: 0,
											time_out_at: 0,
											next_up_on_time_out: None,
											availability: BitVec::default(),
											group_responsible: GroupIndex(index as u32),
											candidate_hash: candidate_receipt.hash(),
											candidate_descriptor: candidate_receipt
												.descriptor
												.clone(),
										}),
										Scheduled => CoreState::Scheduled(ScheduledCore {
											para_id: (index + 1).into(),
											collator: None,
										}),
										Free => todo!(),
									}
								})
								.collect::<Vec<_>>();

							let _ = sender.send(Ok(cores));
						},
						RuntimeApiMessage::Request(
							_request,
							RuntimeApiRequest::CurrentBabeEpoch(sender),
						) => {
							let _ = sender.send(Ok(self
								.state
								.babe_epoch
								.clone()
								.expect("Babe epoch unpopulated")));
						},
						RuntimeApiMessage::Request(
							_block_hash,
							RuntimeApiRequest::AsyncBackingParams(sender),
						) => {
							let _ = sender.send(Ok(AsyncBackingParams {
								max_candidate_depth: self.config.max_candidate_depth,
								allowed_ancestry_len: self.config.allowed_ancestry_len,
							}));
						},
						RuntimeApiMessage::Request(_parent, RuntimeApiRequest::Version(tx)) => {
							tx.send(Ok(RuntimeApiRequest::DISABLED_VALIDATORS_RUNTIME_REQUIREMENT))
								.unwrap();
						},
						RuntimeApiMessage::Request(
							_parent,
							RuntimeApiRequest::DisabledValidators(tx),
						) => {
							tx.send(Ok(vec![])).unwrap();
						},
						RuntimeApiMessage::Request(
							_parent,
							RuntimeApiRequest::MinimumBackingVotes(_session_index, tx),
						) => {
							tx.send(Ok(self.config.minimum_backing_votes)).unwrap();
						},
						RuntimeApiMessage::Request(
							_parent,
							RuntimeApiRequest::ValidatorGroups(tx),
						) => {
							let groups = self.session_info().validator_groups.to_vec();
							let group_rotation_info = GroupRotationInfo {
								session_start_block: 1,
								group_rotation_frequency: 12,
								now: 1,
							};
							tx.send(Ok((groups, group_rotation_info))).unwrap();
						},
						RuntimeApiMessage::Request(
							_parent,
							RuntimeApiRequest::ValidationCodeByHash(_, tx),
						) => {
							let validation_code = ValidationCode(Vec::new());
							if let Err(err) = tx.send(Ok(Some(validation_code))) {
								gum::error!(target: LOG_TARGET, ?err, "validation code wasn't received");
							}
						},
						RuntimeApiMessage::Request(
							_parent,
							RuntimeApiRequest::ApprovalVotingParams(_, tx),
						) =>
							if let Err(err) = tx.send(Ok(ApprovalVotingParams::default())) {
								gum::error!(target: LOG_TARGET, ?err, "Voting params weren't received");
							},
						RuntimeApiMessage::Request(_parent, RuntimeApiRequest::ClaimQueue(tx)) => {
							tx.send(Ok(self.state.claim_queue.clone())).unwrap();
						},
						// Long term TODO: implement more as needed.
						message => {
							unimplemented!("Unexpected runtime-api message: {:?}", message)
						},
					}
				},
			}
		}
	}
}

pub fn default_node_features() -> NodeFeatures {
	let mut node_features = NodeFeatures::new();
	node_features.resize(node_features::FeatureIndex::FirstUnassigned as usize, false);
	node_features.set(node_features::FeatureIndex::AvailabilityChunkMapping as u8 as usize, true);
	node_features.set(node_features::FeatureIndex::ElasticScalingMVP as u8 as usize, true);
	node_features.set(node_features::FeatureIndex::CandidateReceiptV2 as u8 as usize, true);

	node_features
}