Commit 4ff6d8f5 authored by Bernhard Schuster's avatar Bernhard Schuster
Browse files

refactor

parent 22b55584
Pipeline #154734 failed with stages
in 26 minutes and 31 seconds
......@@ -7004,6 +7004,7 @@ dependencies = [
"async-trait",
"color-eyre",
"futures 0.3.16",
"futures-timer 3.0.2",
"parity-util-mem",
"polkadot-cli",
"polkadot-node-core-backing",
......
......@@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
use crate::cli::{Cli, Subcommand};
use crate::cli::{Cli, Subcommand, RunCmd};
use futures::future::TryFutureExt;
use log::info;
use sc_cli::{Role, RuntimeVersion, SubstrateCli};
......@@ -210,8 +210,11 @@ fn ensure_dev(spec: &Box<dyn service::ChainSpec>) -> std::result::Result<(), Str
/// accepts an alternative overseer generator, to adjust behavior
/// for integration tests as needed.
#[cfg(feature = "malus")]
pub fn run_node(cli: Cli, overseer_gen: impl service::OverseerGen) -> Result<()> {
run_node_inner(cli, overseer_gen)
pub fn run_node(run: RunCmd, overseer_gen: impl service::OverseerGen) -> Result<()> {
run_node_inner(Cli {
subcommand: None,
run,
}, overseer_gen)
}
fn run_node_inner(cli: Cli, overseer_gen: impl service::OverseerGen) -> Result<()> {
......
[lib]
name = "malus"
path = "src/lib.rs"
[[bin]]
name = "malus-suggest-garbage-candidate"
path = "src/malus-suggest-garbage-candidate.rs"
[[bin]]
name = "malus-back-garbage-candidate"
path = "src/malus-back-garbage-candidate.rs"
[[bin]]
name = "malus-dispute-ancestor"
path = "src/malus-dispute-ancestor.rs"
[package]
name = "polkadot-test-malus"
description = "Misbehaving nodes for local testnets, system and Simnet tests."
......@@ -25,6 +8,10 @@ edition = "2018"
readme = "README.md"
publish = false
[[bin]]
name = "malus"
path = "src/malus.rs"
[dependencies]
polkadot-cli = { path = "../../cli", default-features = false, features = [ "cli", "malus" ] }
polkadot-node-subsystem = { path = "../subsystem" }
......@@ -43,7 +30,9 @@ structopt = "0.3.21"
async-trait = "0.1.51"
sp-keystore = { git = "https://github.com/paritytech/substrate", branch = "master" }
futures = "0.3.16"
futures-timer = "3.0.2"
tracing = "0.1.26"
[features]
default = []
disputes = ["polkadot-node-core-dispute-coordinator/disputes"]
// Copyright 2021 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! A malus or nemesis node launch code.
use color_eyre::eyre;
use polkadot_cli::RunCmd;
use structopt::StructOpt;
pub(crate) mod shared;
pub(crate) mod interceptor;
mod variants;
use variants::*;
/// Define the different variants of behavior.
#[derive(Debug, StructOpt, PartialEq, Eq)]
enum NemesisVariant {
BackGarbageCandidate,
SuggestGarabageCandidate,
DisputeAncestor,
}
#[derive(Debug, StructOpt)]
#[allow(missing_docs)]
struct MalusCli {
#[structopt(subcommand)]
pub variant: NemesisVariant,
#[structopt(flatten)]
pub run: RunCmd,
}
impl MalusCli {
/// Launch a malus node.
fn launch(self) -> eyre::Result<()> {
let Self {
variant,
run,
} = self;
match variant {
NemesisVariant::BackGarbageCandidate => polkadot_cli::run_node(run, BackGarbageCandidate)?,
NemesisVariant::SuggestGarabageCandidate => polkadot_cli::run_node(run, SuggestGarbageCandidate)?,
NemesisVariant::DisputeAncestor => polkadot_cli::run_node(run, DisputeAncestor)?,
}
Ok(())
}
}
fn main() -> eyre::Result<()> {
color_eyre::install()?;
let cli = MalusCli::from_args();
cli.launch()?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn subcommand_works() {
let cli = MalusCli::from_iter_safe(IntoIterator::into_iter(["malus", "dispute-ancestor"])).unwrap();
assert_matches::assert_matches!(cli, MalusCli {
variant,
..
} => {
assert_eq!(variant, NemesisVariant::DisputeAncestor);
});
}
}
......@@ -24,7 +24,7 @@ pub(crate) const MALICIOUS_POV: &[u8] = "😈😈pov_looks_valid_to_me😈😈".
/// Launch a service task for each item in the provided queue.
#[allow(unused)]
pub(crate) fn launch_processing_task<X, F, U, Q, S>(spawner: S, queue: Q, action: F)
pub(crate) fn launch_processing_task<X, F, U, Q, S>(spawner: &S, queue: Q, action: F)
where
F: Fn(X) -> U + Send + 'static,
U: Future<Output = ()> + Send + 'static,
......@@ -32,14 +32,14 @@ where
X: Send,
S: 'static + SpawnNamed + Clone + Unpin,
{
let spawner2 = spawner.clone();
spawner2.spawn(
let spawner2: S = spawner.clone();
spawner.spawn(
"nemesis-queue-processor",
Box::pin(async move {
let spawner = spawner.clone();
let spawner3 = spawner2.clone();
queue
.for_each(move |input| {
spawner.spawn("nemesis-task", Box::pin(action(input)));
spawner3.spawn("nemesis-task", Box::pin(action(input)));
async move { () }
})
.await;
......
......@@ -19,14 +19,12 @@
#![allow(missing_docs)]
use color_eyre::eyre;
use polkadot_cli::{
create_default_subsystems,
service::{
AuthorityDiscoveryApi, AuxStore, BabeApi, Block, Error, HeaderBackend, Overseer,
OverseerGen, OverseerGenArgs, OverseerHandle, ParachainHost, ProvideRuntimeApi, SpawnNamed,
},
Cli,
};
// Import extra types relevant to the particular
......@@ -37,8 +35,10 @@ use polkadot_node_subsystem::messages::{
};
use polkadot_node_subsystem_util as util;
use util::metrics::Metrics as _;
// Filter wrapping related types.
use malus::*;
use crate::interceptor::*;
use crate::shared::*;
use polkadot_node_primitives::{PoV, ValidationResult};
use polkadot_primitives::v1::{
......@@ -48,13 +48,6 @@ use polkadot_primitives::v1::{
use futures::channel::oneshot;
use std::sync::{Arc, Mutex};
use structopt::StructOpt;
use shared::*;
mod shared;
use std::collections::HashMap;
#[derive(Clone, Debug)]
......@@ -195,7 +188,7 @@ where
}
/// Generates an overseer that exposes bad behavior.
struct BackGarbageCandidate;
pub(crate) struct BackGarbageCandidate;
impl OverseerGen for BackGarbageCandidate {
fn generate<'a, Spawner, RuntimeClient>(
......@@ -237,11 +230,3 @@ impl OverseerGen for BackGarbageCandidate {
Ok((overseer, handle))
}
}
fn main() -> eyre::Result<()> {
color_eyre::install()?;
let cli = Cli::from_args();
assert_matches::assert_matches!(cli.subcommand, None);
polkadot_cli::run_node(cli, BackGarbageCandidate)?;
Ok(())
}
......@@ -21,18 +21,18 @@
#![allow(missing_docs)]
use color_eyre::eyre;
use polkadot_cli::{
create_default_subsystems,
service::{
AuthorityDiscoveryApi, AuxStore, BabeApi, Block, Error, HeaderBackend, Overseer,
OverseerGen, OverseerGenArgs, OverseerHandle, ParachainHost, ProvideRuntimeApi, SpawnNamed,
},
Cli,
};
// Filter wrapping related types.
use malus::*;
use crate::interceptor::*;
use crate::shared::*;
use polkadot_node_subsystem::overseer::SubsystemSender;
// Import extra types relevant to the particular
// subsystem.
......@@ -43,15 +43,9 @@ use polkadot_primitives::v1::CandidateReceipt;
use sp_keystore::SyncCryptoStorePtr;
use util::{metered, metrics::Metrics as _};
// Filter wrapping related types.
use malus::overseer::SubsystemSender;
use shared::*;
use std::sync::Arc;
use structopt::StructOpt;
mod shared;
use std::time::Duration;
use futures_timer::Delay;
/// Become Loki and throw in a dispute once in a while, for an unfinalized block.
#[derive(Clone, Debug)]
......@@ -85,7 +79,6 @@ where
// make that disappear, and instead craft our own message.
msg: CandidateBackingMessage::Second(_, ccr, _),
} => {
// TODO FIXME avoid this, by moving all the logic in here by providing access to the spawner.
self.sink.unbounded_send((sender.clone(), ccr)).unwrap();
None
},
......@@ -99,9 +92,9 @@ where
}
/// Generates an overseer that disputes every ancestor.
struct DisputeEverything;
pub(crate) struct DisputeAncestor;
impl OverseerGen for DisputeEverything {
impl OverseerGen for DisputeAncestor {
fn generate<'a, Spawner, RuntimeClient>(
&self,
args: OverseerGenArgs<'a, Spawner, RuntimeClient>,
......@@ -137,7 +130,7 @@ impl OverseerGen for DisputeEverything {
Overseer::new(leaves, all_subsystems, registry, runtime_client, spawner.clone())?;
launch_processing_task(
spawner.clone(),
&spawner,
source,
|(mut subsystem_sender, candidate_receipt): (_, CandidateReceipt)| async move {
let relay_parent = candidate_receipt.descriptor().relay_parent;
......@@ -155,8 +148,7 @@ impl OverseerGen for DisputeEverything {
relay_parent,
);
// consider adding a delay here
// Delay::new(Duration::from_secs(12)).await;
Delay::new(Duration::from_secs(12)).await;
// 😈
let msg = DisputeCoordinatorMessage::IssueLocalStatement(
......@@ -173,11 +165,3 @@ impl OverseerGen for DisputeEverything {
Ok((overseer, handle))
}
}
fn main() -> eyre::Result<()> {
color_eyre::install()?;
let cli = Cli::from_args();
assert_matches::assert_matches!(cli.subcommand, None);
polkadot_cli::run_node(cli, DisputeEverything)?;
Ok(())
}
// Copyright 2021 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! Collection of behavior variants.
mod back_garbage_candidate;
mod suggest_garbage_candidate;
mod dispute_ancestor;
pub(crate) use self::back_garbage_candidate::BackGarbageCandidate;
pub(crate) use self::suggest_garbage_candidate::SuggestGarbageCandidate;
pub(crate) use self::dispute_ancestor::DisputeAncestor;
......@@ -22,25 +22,23 @@
#![allow(missing_docs)]
use color_eyre::eyre;
use malus::overseer::SubsystemSender;
use polkadot_cli::{
create_default_subsystems,
service::{
AuthorityDiscoveryApi, AuxStore, BabeApi, Block, Error, HeaderBackend, Overseer,
OverseerGen, OverseerGenArgs, OverseerHandle, ParachainHost, ProvideRuntimeApi, SpawnNamed,
},
Cli,
};
// Import extra types relevant to the particular
// subsystem.
use polkadot_node_core_backing::{CandidateBackingSubsystem, Metrics};
use polkadot_node_primitives::{BlockData, PoV, Statement};
use polkadot_node_primitives::Statement;
use polkadot_node_subsystem::messages::{CandidateBackingMessage, StatementDistributionMessage};
use polkadot_node_subsystem::overseer::{self, SubsystemSender};
use polkadot_node_subsystem_util as util;
// Filter wrapping related types.
use malus::*;
use crate::interceptor::*;
use polkadot_primitives::v1::{
CandidateCommitments, CandidateReceipt, CommittedCandidateReceipt, CompactStatement, Hash,
Signed,
......@@ -50,11 +48,8 @@ use util::{metered, metrics::Metrics as _};
use std::sync::Arc;
use structopt::StructOpt;
use crate::shared::*;
use shared::*;
mod shared;
/// Replaces the seconded PoV data
/// of outgoing messages by some garbage data.
#[derive(Clone)]
......@@ -97,7 +92,7 @@ where
}
/// Generates an overseer that exposes bad behavior.
struct SuggestGarbageCandidate;
pub(crate) struct SuggestGarbageCandidate;
impl OverseerGen for SuggestGarbageCandidate {
fn generate<'a, Spawner, RuntimeClient>(
......@@ -136,7 +131,7 @@ impl OverseerGen for SuggestGarbageCandidate {
Overseer::new(leaves, all_subsystems, registry, runtime_client, spawner.clone())?;
launch_processing_task(
spawner,
&spawner,
source,
move |(mut subsystem_sender, hash, candidate_receipt): (_, Hash, CandidateReceipt)| {
let keystore = keystore.clone();
......@@ -178,11 +173,3 @@ impl OverseerGen for SuggestGarbageCandidate {
Ok((overseer, handle))
}
}
fn main() -> eyre::Result<()> {
color_eyre::install()?;
let cli = Cli::from_args();
assert_matches::assert_matches!(cli.subcommand, None);
polkadot_cli::run_node(cli, SuggestGarbageCandidate)?;
Ok(())
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment