referrerpolicy=no-referrer-when-downgrade
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Copyright (C) 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 clap::Parser;
use color_eyre::eyre;

pub(crate) mod interceptor;
pub(crate) mod shared;

mod variants;

use variants::*;

/// Define the different variants of behavior.
#[derive(Debug, Parser)]
#[command(about = "Malus - the nemesis of polkadot.", version, rename_all = "kebab-case")]
enum NemesisVariant {
	/// Suggest a candidate with an invalid proof of validity.
	SuggestGarbageCandidate(SuggestGarbageCandidateOptions),
	/// Support disabled validators in backing and statement distribution.
	SupportDisabled(SupportDisabledOptions),
	/// Back a candidate with a specifically crafted proof of validity.
	BackGarbageCandidate(BackGarbageCandidateOptions),
	/// Delayed disputing of ancestors that are perfectly fine.
	DisputeAncestor(DisputeAncestorOptions),
	/// Delayed disputing of finalized candidates.
	DisputeFinalizedCandidates(DisputeFinalizedCandidatesOptions),
	/// Spam many request statements instead of sending a single one.
	SpamStatementRequests(SpamStatementRequestsOptions),
}

#[derive(Debug, Parser)]
#[allow(missing_docs)]
struct MalusCli {
	#[command(subcommand)]
	pub variant: NemesisVariant,
	/// Sets the minimum delay between the best and finalized block.
	pub finality_delay: Option<u32>,
}

impl MalusCli {
	/// Launch a malus node.
	fn launch(self) -> eyre::Result<()> {
		let finality_delay = self.finality_delay;
		match self.variant {
			NemesisVariant::BackGarbageCandidate(opts) => {
				let BackGarbageCandidateOptions { percentage, cli } = opts;

				polkadot_cli::run_node(cli, BackGarbageCandidates { percentage }, finality_delay)?
			},
			NemesisVariant::SuggestGarbageCandidate(opts) => {
				let SuggestGarbageCandidateOptions { percentage, cli } = opts;

				polkadot_cli::run_node(
					cli,
					SuggestGarbageCandidates { percentage },
					finality_delay,
				)?
			},
			NemesisVariant::SupportDisabled(opts) => {
				let SupportDisabledOptions { cli } = opts;

				polkadot_cli::run_node(cli, SupportDisabled, finality_delay)?
			},
			NemesisVariant::DisputeAncestor(opts) => {
				let DisputeAncestorOptions {
					fake_validation,
					fake_validation_error,
					percentage,
					cli,
				} = opts;

				polkadot_cli::run_node(
					cli,
					DisputeValidCandidates { fake_validation, fake_validation_error, percentage },
					finality_delay,
				)?
			},
			NemesisVariant::DisputeFinalizedCandidates(opts) => {
				let DisputeFinalizedCandidatesOptions { dispute_offset, cli } = opts;

				polkadot_cli::run_node(
					cli,
					DisputeFinalizedCandidates { dispute_offset },
					finality_delay,
				)?
			},
			NemesisVariant::SpamStatementRequests(opts) => {
				let SpamStatementRequestsOptions { spam_factor, cli } = opts;

				polkadot_cli::run_node(cli, SpamStatementRequests { spam_factor }, finality_delay)?
			},
		}
		Ok(())
	}
}

fn main() -> eyre::Result<()> {
	color_eyre::install()?;
	let cli = MalusCli::parse();
	cli.launch()?;
	Ok(())
}

#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	fn subcommand_works() {
		let cli = MalusCli::try_parse_from(IntoIterator::into_iter([
			"malus",
			"dispute-ancestor",
			"--bob",
		]))
		.unwrap();
		assert_matches::assert_matches!(cli, MalusCli {
			variant: NemesisVariant::DisputeAncestor(run),
			..
		} => {
			assert!(run.cli.run.base.bob);
		});
	}

	#[test]
	fn percentage_works_suggest_garbage() {
		let cli = MalusCli::try_parse_from(IntoIterator::into_iter([
			"malus",
			"suggest-garbage-candidate",
			"--percentage",
			"100",
			"--bob",
		]))
		.unwrap();
		assert_matches::assert_matches!(cli, MalusCli {
			variant: NemesisVariant::SuggestGarbageCandidate(run),
			..
		} => {
			assert!(run.cli.run.base.bob);
		});
	}

	#[test]
	fn percentage_works_dispute_ancestor() {
		let cli = MalusCli::try_parse_from(IntoIterator::into_iter([
			"malus",
			"dispute-ancestor",
			"--percentage",
			"100",
			"--bob",
		]))
		.unwrap();
		assert_matches::assert_matches!(cli, MalusCli {
			variant: NemesisVariant::DisputeAncestor(run),
			..
		} => {
			assert!(run.cli.run.base.bob);
		});
	}

	#[test]
	fn percentage_works_back_garbage() {
		let cli = MalusCli::try_parse_from(IntoIterator::into_iter([
			"malus",
			"back-garbage-candidate",
			"--percentage",
			"100",
			"--bob",
		]))
		.unwrap();
		assert_matches::assert_matches!(cli, MalusCli {
			variant: NemesisVariant::BackGarbageCandidate(run),
			..
		} => {
			assert!(run.cli.run.base.bob);
		});
	}

	#[test]
	#[should_panic]
	fn validate_range_for_percentage() {
		let cli = MalusCli::try_parse_from(IntoIterator::into_iter([
			"malus",
			"suggest-garbage-candidate",
			"--percentage",
			"101",
			"--bob",
		]))
		.unwrap();
		assert_matches::assert_matches!(cli, MalusCli {
			variant: NemesisVariant::DisputeAncestor(run),
			..
		} => {
			assert!(run.cli.run.base.bob);
		});
	}

	#[test]
	fn dispute_finalized_candidates_works() {
		let cli = MalusCli::try_parse_from(IntoIterator::into_iter([
			"malus",
			"dispute-finalized-candidates",
			"--bob",
		]))
		.unwrap();
		assert_matches::assert_matches!(cli, MalusCli {
			variant: NemesisVariant::DisputeFinalizedCandidates(run),
			..
		} => {
			assert!(run.cli.run.base.bob);
		});
	}

	#[test]
	fn dispute_finalized_offset_value_works() {
		let cli = MalusCli::try_parse_from(IntoIterator::into_iter([
			"malus",
			"dispute-finalized-candidates",
			"--dispute-offset",
			"13",
			"--bob",
		]))
		.unwrap();
		assert_matches::assert_matches!(cli, MalusCli {
			variant: NemesisVariant::DisputeFinalizedCandidates(opts),
			..
		} => {
			assert_eq!(opts.dispute_offset, 13); // This line checks that dispute_offset is correctly set to 13
			assert!(opts.cli.run.base.bob);
		});
	}
}