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
pub use sc_network::ReputationChange;

/// Unified annoyance cost and good behavior benefits.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(missing_docs)]
pub enum UnifiedReputationChange {
	CostMajor(&'static str),
	CostMinor(&'static str),
	CostMajorRepeated(&'static str),
	CostMinorRepeated(&'static str),
	Malicious(&'static str),
	BenefitMinorFirst(&'static str),
	BenefitMinor(&'static str),
	BenefitMajorFirst(&'static str),
	BenefitMajor(&'static str),
}

impl UnifiedReputationChange {
	/// Obtain the cost or benefit associated with
	/// the enum variant.
	///
	/// Order of magnitude rationale:
	///
	/// * the peerset will not connect to a peer whose reputation is below a fixed value
	/// * `max(2% *$rep, 1)` is the delta of convergence towards a reputation of 0
	///
	/// The whole range of an `i32` should be used, so order of magnitude of
	/// something malicious should be `1<<20` (give or take).
	const fn cost_or_benefit(&self) -> i32 {
		match self {
			Self::CostMinor(_) => -100_000,
			Self::CostMajor(_) => -300_000,
			Self::CostMinorRepeated(_) => -200_000,
			Self::CostMajorRepeated(_) => -600_000,
			Self::Malicious(_) => i32::MIN,
			Self::BenefitMajorFirst(_) => 300_000,
			Self::BenefitMajor(_) => 200_000,
			Self::BenefitMinorFirst(_) => 15_000,
			Self::BenefitMinor(_) => 10_000,
		}
	}

	/// Extract the static description.
	pub const fn description(&self) -> &'static str {
		match self {
			Self::CostMinor(description) => description,
			Self::CostMajor(description) => description,
			Self::CostMinorRepeated(description) => description,
			Self::CostMajorRepeated(description) => description,
			Self::Malicious(description) => description,
			Self::BenefitMajorFirst(description) => description,
			Self::BenefitMajor(description) => description,
			Self::BenefitMinorFirst(description) => description,
			Self::BenefitMinor(description) => description,
		}
	}

	/// Whether the reputation change is for good behavior.
	pub const fn is_benefit(&self) -> bool {
		match self {
			Self::BenefitMajorFirst(_) |
			Self::BenefitMajor(_) |
			Self::BenefitMinorFirst(_) |
			Self::BenefitMinor(_) => true,
			_ => false,
		}
	}

	/// Convert into a base reputation as used with substrate.
	pub const fn into_base_rep(self) -> ReputationChange {
		ReputationChange::new(self.cost_or_benefit(), self.description())
	}
}