referrerpolicy=no-referrer-when-downgrade

polkadot_subsystem_bench/mock/
availability_recovery.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 availability recovery suitable to be used in benchmarks.
18
19use std::sync::Arc;
20
21use futures::FutureExt;
22use polkadot_node_primitives::{AvailableData, BlockData, PoV};
23use polkadot_node_subsystem::{
24	messages::AvailabilityRecoveryMessage, overseer, SpawnedSubsystem, SubsystemError,
25};
26use polkadot_node_subsystem_types::OverseerSignal;
27use polkadot_primitives::{Hash, HeadData, PersistedValidationData};
28
29const LOG_TARGET: &str = "subsystem-bench::availability-recovery-mock";
30
31pub struct MockAvailabilityRecovery {}
32
33impl MockAvailabilityRecovery {
34	pub fn new() -> Self {
35		Self {}
36	}
37}
38
39#[overseer::subsystem(AvailabilityRecovery, error=SubsystemError, prefix=self::overseer)]
40impl<Context> MockAvailabilityRecovery {
41	fn start(self, ctx: Context) -> SpawnedSubsystem {
42		let future = self.run(ctx).map(|_| Ok(())).boxed();
43
44		SpawnedSubsystem { name: "test-environment", future }
45	}
46}
47
48#[overseer::contextbounds(AvailabilityRecovery, prefix = self::overseer)]
49impl MockAvailabilityRecovery {
50	async fn run<Context>(self, mut ctx: Context) {
51		loop {
52			let msg = ctx.recv().await.expect("Overseer never fails us");
53			match msg {
54				orchestra::FromOrchestra::Signal(signal) =>
55					if signal == OverseerSignal::Conclude {
56						return
57					},
58				orchestra::FromOrchestra::Communication { msg } => match msg {
59					AvailabilityRecoveryMessage::RecoverAvailableData(receipt, _, _, _, tx) => {
60						gum::debug!(target: LOG_TARGET, "RecoverAvailableData for candidate {:?}", receipt.hash());
61						let available_data = AvailableData {
62							pov: Arc::new(PoV { block_data: BlockData(Vec::new()) }),
63							validation_data: PersistedValidationData {
64								parent_head: HeadData(Vec::new()),
65								relay_parent_number: 0,
66								relay_parent_storage_root: Hash::default(),
67								max_pov_size: 2,
68							},
69						};
70						tx.send(Ok(available_data)).unwrap();
71					},
72				},
73			}
74		}
75	}
76}