referrerpolicy=no-referrer-when-downgrade

malus/variants/
back_garbage_candidate.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//! This variant of Malus backs/approves all malicious candidates crafted by
18//! `suggest-garbage-candidate` variant and behaves honestly with other
19//! candidates.
20
21use polkadot_cli::{
22	service::{
23		AuxStore, Error, ExtendedOverseerGenArgs, Overseer, OverseerConnector, OverseerGen,
24		OverseerGenArgs, OverseerHandle,
25	},
26	validator_overseer_builder, Cli,
27};
28use polkadot_node_subsystem::SpawnGlue;
29use polkadot_node_subsystem_types::{ChainApiBackend, RuntimeApiSubsystemClient};
30use sp_core::traits::SpawnNamed;
31
32use crate::{
33	interceptor::*,
34	variants::{FakeCandidateValidation, FakeCandidateValidationError, ReplaceValidationResult},
35};
36
37use std::sync::Arc;
38
39#[derive(Debug, clap::Parser)]
40#[clap(rename_all = "kebab-case")]
41#[allow(missing_docs)]
42pub struct BackGarbageCandidateOptions {
43	/// Determines the percentage of garbage candidates that should be backed.
44	/// Defaults to 100% of garbage candidates being backed.
45	#[clap(short, long, ignore_case = true, default_value_t = 100, value_parser = clap::value_parser!(u8).range(0..=100))]
46	pub percentage: u8,
47
48	#[clap(flatten)]
49	pub cli: Cli,
50}
51
52/// Generates an overseer that replaces the candidate validation subsystem with our malicious
53/// variant.
54pub(crate) struct BackGarbageCandidates {
55	/// The probability of behaving maliciously.
56	pub percentage: u8,
57}
58
59impl OverseerGen for BackGarbageCandidates {
60	fn generate<Spawner, RuntimeClient>(
61		&self,
62		connector: OverseerConnector,
63		args: OverseerGenArgs<'_, Spawner, RuntimeClient>,
64		ext_args: Option<ExtendedOverseerGenArgs>,
65	) -> Result<(Overseer<SpawnGlue<Spawner>, Arc<RuntimeClient>>, OverseerHandle), Error>
66	where
67		RuntimeClient: RuntimeApiSubsystemClient + ChainApiBackend + AuxStore + 'static,
68		Spawner: 'static + SpawnNamed + Clone + Unpin,
69	{
70		let validation_filter = ReplaceValidationResult::new(
71			FakeCandidateValidation::BackingAndApprovalValid,
72			FakeCandidateValidationError::InvalidOutputs,
73			f64::from(self.percentage),
74		);
75
76		validator_overseer_builder(
77			args,
78			ext_args.expect("Extended arguments required to build validator overseer are provided"),
79		)?
80		.replace_candidate_validation(move |cv_subsystem| {
81			InterceptedSubsystem::new(cv_subsystem, validation_filter)
82		})
83		.build_with_connector(connector)
84		.map_err(|e| e.into())
85	}
86}