referrerpolicy=no-referrer-when-downgrade

polkadot_node_network_protocol/
reputation.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
17pub use sc_network::ReputationChange;
18
19/// Unified annoyance cost and good behavior benefits.
20#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21#[allow(missing_docs)]
22pub enum UnifiedReputationChange {
23	CostMajor(&'static str),
24	CostMinor(&'static str),
25	CostMajorRepeated(&'static str),
26	CostMinorRepeated(&'static str),
27	Malicious(&'static str),
28	BenefitMinorFirst(&'static str),
29	BenefitMinor(&'static str),
30	BenefitMajorFirst(&'static str),
31	BenefitMajor(&'static str),
32}
33
34impl UnifiedReputationChange {
35	/// Obtain the cost or benefit associated with
36	/// the enum variant.
37	///
38	/// Order of magnitude rationale:
39	///
40	/// * the peerset will not connect to a peer whose reputation is below a fixed value
41	/// * `max(2% *$rep, 1)` is the delta of convergence towards a reputation of 0
42	///
43	/// The whole range of an `i32` should be used, so order of magnitude of
44	/// something malicious should be `1<<20` (give or take).
45	pub const fn cost_or_benefit(&self) -> i32 {
46		match self {
47			Self::CostMinor(_) => -100_000,
48			Self::CostMajor(_) => -300_000,
49			Self::CostMinorRepeated(_) => -200_000,
50			Self::CostMajorRepeated(_) => -600_000,
51			Self::Malicious(_) => i32::MIN,
52			Self::BenefitMajorFirst(_) => 300_000,
53			Self::BenefitMajor(_) => 200_000,
54			Self::BenefitMinorFirst(_) => 15_000,
55			Self::BenefitMinor(_) => 10_000,
56		}
57	}
58
59	/// Extract the static description.
60	pub const fn description(&self) -> &'static str {
61		match self {
62			Self::CostMinor(description) => description,
63			Self::CostMajor(description) => description,
64			Self::CostMinorRepeated(description) => description,
65			Self::CostMajorRepeated(description) => description,
66			Self::Malicious(description) => description,
67			Self::BenefitMajorFirst(description) => description,
68			Self::BenefitMajor(description) => description,
69			Self::BenefitMinorFirst(description) => description,
70			Self::BenefitMinor(description) => description,
71		}
72	}
73
74	/// Whether the reputation change is for good behavior.
75	pub const fn is_benefit(&self) -> bool {
76		match self {
77			Self::BenefitMajorFirst(_) |
78			Self::BenefitMajor(_) |
79			Self::BenefitMinorFirst(_) |
80			Self::BenefitMinor(_) => true,
81			_ => false,
82		}
83	}
84}
85
86impl From<UnifiedReputationChange> for ReputationChange {
87	fn from(value: UnifiedReputationChange) -> Self {
88		ReputationChange::new(value.cost_or_benefit(), value.description())
89	}
90}