referrerpolicy=no-referrer-when-downgrade

polkadot_subsystem_bench/approval/
mock_chain_selection.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
17use crate::approval::{ApprovalTestState, PastSystemClock, LOG_TARGET, SLOT_DURATION_MILLIS};
18use futures::FutureExt;
19use polkadot_node_primitives::approval::time::{slot_number_to_tick, Clock, TICK_DURATION_MILLIS};
20use polkadot_node_subsystem::{overseer, SpawnedSubsystem, SubsystemError};
21use polkadot_node_subsystem_types::messages::ChainSelectionMessage;
22
23/// Mock ChainSelection subsystem used to answer request made by the approval-voting subsystem,
24/// during benchmark. All the necessary information to answer the requests is stored in the `state`
25pub struct MockChainSelection {
26	pub state: ApprovalTestState,
27	pub clock: PastSystemClock,
28}
29
30#[overseer::subsystem(ChainSelection, error=SubsystemError, prefix=self::overseer)]
31impl<Context> MockChainSelection {
32	fn start(self, ctx: Context) -> SpawnedSubsystem {
33		let future = self.run(ctx).map(|_| Ok(())).boxed();
34
35		SpawnedSubsystem { name: "mock-chain-subsystem", future }
36	}
37}
38
39#[overseer::contextbounds(ChainSelection, prefix = self::overseer)]
40impl MockChainSelection {
41	async fn run<Context>(self, mut ctx: Context) {
42		loop {
43			let msg = ctx.recv().await.expect("Should not fail");
44			match msg {
45				orchestra::FromOrchestra::Signal(_) => {},
46				orchestra::FromOrchestra::Communication { msg } =>
47					if let ChainSelectionMessage::Approved(hash) = msg {
48						let block_info = self.state.get_info_by_hash(hash);
49						let approved_number = block_info.block_number;
50
51						block_info.approved.store(true, std::sync::atomic::Ordering::SeqCst);
52						self.state
53							.last_approved_block
54							.store(approved_number, std::sync::atomic::Ordering::SeqCst);
55
56						let approved_in_tick = self.clock.tick_now() -
57							slot_number_to_tick(SLOT_DURATION_MILLIS, block_info.slot);
58
59						gum::info!(target: LOG_TARGET, ?hash, "Chain selection approved  after {:} ms", approved_in_tick * TICK_DURATION_MILLIS);
60					},
61			}
62		}
63	}
64}