Skip to content

Commit

Permalink
Remove unused
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiEres committed Sep 23, 2024
1 parent 1b28f37 commit 239c401
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 84 deletions.
9 changes: 2 additions & 7 deletions polkadot/node/malus/src/malus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,11 @@ impl MalusCli {
polkadot_cli::run_node(cli, SupportDisabled, finality_delay)?
},
NemesisVariant::DisputeAncestor(opts) => {
let DisputeAncestorOptions {
fake_validation,
fake_validation_error,
percentage,
cli,
} = opts;
let DisputeAncestorOptions { fake_validation, percentage, cli } = opts;

polkadot_cli::run_node(
cli,
DisputeValidCandidates { fake_validation, fake_validation_error, percentage },
DisputeValidCandidates { fake_validation, percentage },
finality_delay,
)?
},
Expand Down
5 changes: 1 addition & 4 deletions polkadot/node/malus/src/variants/back_garbage_candidate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use sp_core::traits::SpawnNamed;

use crate::{
interceptor::*,
variants::{FakeCandidateValidation, FakeCandidateValidationError, ReplaceValidationResult},
variants::{FakeCandidateValidation, ReplaceValidationResult},
};

use std::sync::Arc;
Expand Down Expand Up @@ -67,12 +67,9 @@ impl OverseerGen for BackGarbageCandidates {
RuntimeClient: RuntimeApiSubsystemClient + ChainApiBackend + AuxStore + 'static,
Spawner: 'static + SpawnNamed + Clone + Unpin,
{
let spawner = args.spawner.clone();
let validation_filter = ReplaceValidationResult::new(
FakeCandidateValidation::BackingAndApprovalValid,
FakeCandidateValidationError::InvalidOutputs,
f64::from(self.percentage),
SpawnGlue(spawner),
);

validator_overseer_builder(
Expand Down
57 changes: 6 additions & 51 deletions polkadot/node/malus/src/variants/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use crate::{
shared::{MALICIOUS_POV, MALUS},
};

use polkadot_node_core_candidate_validation::find_validation_data;
use polkadot_node_primitives::{InvalidCandidate, ValidationResult};

use polkadot_primitives::{
Expand Down Expand Up @@ -149,59 +148,16 @@ impl Into<InvalidCandidate> for FakeCandidateValidationError {
#[derive(Clone, Debug)]
/// An interceptor which fakes validation result with a preconfigured result.
/// Replaces `CandidateValidationSubsystem`.
pub struct ReplaceValidationResult<Spawner> {
pub struct ReplaceValidationResult {
fake_validation: FakeCandidateValidation,
fake_validation_error: FakeCandidateValidationError,
distribution: Bernoulli,
spawner: Spawner,
}

impl<Spawner> ReplaceValidationResult<Spawner>
where
Spawner: overseer::gen::Spawner,
{
pub fn new(
fake_validation: FakeCandidateValidation,
fake_validation_error: FakeCandidateValidationError,
percentage: f64,
spawner: Spawner,
) -> Self {
impl ReplaceValidationResult {
pub fn new(fake_validation: FakeCandidateValidation, percentage: f64) -> Self {
let distribution = Bernoulli::new(percentage / 100.0)
.expect("Invalid probability! Percentage must be in range [0..=100].");
Self { fake_validation, fake_validation_error, distribution, spawner }
}

/// Creates and sends the validation response for a given candidate. Queries the runtime to
/// obtain the validation data for the given candidate.
pub fn send_validation_response<Sender>(
&self,
candidate_descriptor: CandidateDescriptor,
subsystem_sender: Sender,
response_sender: oneshot::Sender<Result<ValidationResult, ValidationFailed>>,
) where
Sender: overseer::CandidateValidationSenderTrait + Clone + Send + 'static,
{
let _candidate_descriptor = candidate_descriptor.clone();
let mut subsystem_sender = subsystem_sender.clone();
let (sender, receiver) = std::sync::mpsc::channel();
self.spawner.spawn_blocking(
"malus-get-validation-data",
Some("malus"),
Box::pin(async move {
match find_validation_data(&mut subsystem_sender, &_candidate_descriptor).await {
Ok(Some((validation_data, validation_code))) => {
sender
.send((validation_data, validation_code))
.expect("channel is still open");
},
_ => {
panic!("Unable to fetch validation data");
},
}
}),
);
let (validation_data, _) = receiver.recv().unwrap();
create_validation_response(validation_data, candidate_descriptor, response_sender);
Self { fake_validation, distribution }
}
}

Expand Down Expand Up @@ -251,18 +207,17 @@ fn create_validation_response(
response_sender.send(result).unwrap();
}

impl<Sender, Spawner> MessageInterceptor<Sender> for ReplaceValidationResult<Spawner>
impl<Sender> MessageInterceptor<Sender> for ReplaceValidationResult
where
Sender: overseer::CandidateValidationSenderTrait + Clone + Send + 'static,
Spawner: overseer::gen::Spawner + Clone + 'static,
{
type Message = CandidateValidationMessage;

// Capture all (approval and backing) candidate validation requests and depending on
// configuration fail them.
fn intercept_incoming(
&self,
subsystem_sender: &mut Sender,
_subsystem_sender: &mut Sender,
msg: FromOrchestra<Self::Message>,
) -> Option<FromOrchestra<Self::Message>> {
match msg {
Expand Down
22 changes: 4 additions & 18 deletions polkadot/node/malus/src/variants/dispute_valid_candidates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use polkadot_node_subsystem_types::{ChainApiBackend, RuntimeApiSubsystemClient};
use sp_core::traits::SpawnNamed;

// Filter wrapping related types.
use super::common::{FakeCandidateValidation, FakeCandidateValidationError};
use super::common::FakeCandidateValidation;
use crate::{interceptor::*, variants::ReplaceValidationResult};

use std::sync::Arc;
Expand All @@ -44,17 +44,10 @@ use std::sync::Arc;
#[allow(missing_docs)]
pub struct DisputeAncestorOptions {
/// Malicious candidate validation subsystem configuration. When enabled, node PVF execution is
/// skipped during backing and/or approval and it's result can by specified by this option and
/// `--fake-validation-error` for invalid candidate outcomes.
/// skipped during backing and/or approval and it's result can be specified by this option
#[arg(long, value_enum, ignore_case = true, default_value_t = FakeCandidateValidation::BackingAndApprovalInvalid)]
pub fake_validation: FakeCandidateValidation,

/// Applies only when `--fake-validation` is configured to reject candidates as invalid. It
/// allows to specify the exact error to return from the malicious candidate validation
/// subsystem.
#[arg(long, value_enum, ignore_case = true, default_value_t = FakeCandidateValidationError::InvalidOutputs)]
pub fake_validation_error: FakeCandidateValidationError,

/// Determines the percentage of candidates that should be disputed. Allows for fine-tuning
/// the intensity of the behavior of the malicious node. Value must be in the range [0..=100].
#[clap(short, long, ignore_case = true, default_value_t = 100, value_parser = clap::value_parser!(u8).range(0..=100))]
Expand All @@ -67,8 +60,6 @@ pub struct DisputeAncestorOptions {
pub(crate) struct DisputeValidCandidates {
/// Fake validation config (applies to disputes as well).
pub fake_validation: FakeCandidateValidation,
/// Fake validation error config.
pub fake_validation_error: FakeCandidateValidationError,
/// The probability of behaving maliciously.
pub percentage: u8,
}
Expand All @@ -84,13 +75,8 @@ impl OverseerGen for DisputeValidCandidates {
RuntimeClient: RuntimeApiSubsystemClient + ChainApiBackend + AuxStore + 'static,
Spawner: 'static + SpawnNamed + Clone + Unpin,
{
let spawner = args.spawner.clone();
let validation_filter = ReplaceValidationResult::new(
self.fake_validation,
self.fake_validation_error,
f64::from(self.percentage),
SpawnGlue(spawner.clone()),
);
let validation_filter =
ReplaceValidationResult::new(self.fake_validation, f64::from(self.percentage));

validator_overseer_builder(
args,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ use crate::{
interceptor::*,
shared::{MALICIOUS_POV, MALUS},
variants::{
create_fake_candidate_commitments, FakeCandidateValidation, FakeCandidateValidationError,
ReplaceValidationResult,
create_fake_candidate_commitments, FakeCandidateValidation, ReplaceValidationResult,
},
};

Expand Down Expand Up @@ -313,9 +312,7 @@ impl OverseerGen for SuggestGarbageCandidates {
let fake_valid_probability = 100.0;
let validation_filter = ReplaceValidationResult::new(
FakeCandidateValidation::BackingAndApprovalValid,
FakeCandidateValidationError::InvalidOutputs,
fake_valid_probability,
SpawnGlue(args.spawner.clone()),
);

validator_overseer_builder(
Expand Down

0 comments on commit 239c401

Please sign in to comment.