referrerpolicy=no-referrer-when-downgrade

polkadot_subsystem_bench/mock/
candidate_validation.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//! A generic mock candidate validation subsystem suitable for using in benchmarks, it
18//! is responding with candidate valid for every request.
19
20use futures::FutureExt;
21use polkadot_node_primitives::ValidationResult;
22use polkadot_node_subsystem::{
23	messages::CandidateValidationMessage, overseer, SpawnedSubsystem, SubsystemError,
24};
25use polkadot_node_subsystem_types::OverseerSignal;
26use polkadot_primitives::{CandidateCommitments, Hash, HeadData, PersistedValidationData};
27
28const LOG_TARGET: &str = "subsystem-bench::candidate-validation-mock";
29
30pub struct MockCandidateValidation {}
31
32impl MockCandidateValidation {
33	pub fn new() -> Self {
34		Self {}
35	}
36}
37
38#[overseer::subsystem(CandidateValidation, error=SubsystemError, prefix=self::overseer)]
39impl<Context> MockCandidateValidation {
40	fn start(self, ctx: Context) -> SpawnedSubsystem {
41		let future = self.run(ctx).map(|_| Ok(())).boxed();
42
43		SpawnedSubsystem { name: "test-environment", future }
44	}
45}
46
47#[overseer::contextbounds(CandidateValidation, prefix = self::overseer)]
48impl MockCandidateValidation {
49	async fn run<Context>(self, mut ctx: Context) {
50		loop {
51			let msg = ctx.recv().await.expect("Overseer never fails us");
52			match msg {
53				orchestra::FromOrchestra::Signal(signal) =>
54					if signal == OverseerSignal::Conclude {
55						return
56					},
57				orchestra::FromOrchestra::Communication { msg } => match msg {
58					CandidateValidationMessage::ValidateFromExhaustive {
59						response_sender,
60						validation_data,
61						..
62					} => {
63						gum::debug!(target: LOG_TARGET, "ValidateFromExhaustive, PVD hash {:?}", validation_data.hash());
64						response_sender
65							.send(Ok(ValidationResult::Valid(
66								CandidateCommitments::default(),
67								PersistedValidationData {
68									parent_head: HeadData(Vec::new()),
69									relay_parent_number: 0,
70									relay_parent_storage_root: Hash::default(),
71									max_pov_size: 2,
72								},
73							)))
74							.unwrap()
75					},
76					_ => unimplemented!("Unexpected chain-api message"),
77				},
78			}
79		}
80	}
81}