referrerpolicy=no-referrer-when-downgrade

pallet_conviction_voting/
conviction.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! The conviction datatype.
19
20use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
21use scale_info::TypeInfo;
22use sp_runtime::{
23	traits::{Bounded, CheckedDiv, CheckedMul, Zero},
24	RuntimeDebug,
25};
26
27use crate::types::Delegations;
28
29/// A value denoting the strength of conviction of a vote.
30#[derive(
31	Encode,
32	Decode,
33	DecodeWithMemTracking,
34	Copy,
35	Clone,
36	Eq,
37	PartialEq,
38	Ord,
39	PartialOrd,
40	RuntimeDebug,
41	TypeInfo,
42	MaxEncodedLen,
43)]
44pub enum Conviction {
45	/// 0.1x votes, unlocked.
46	None,
47	/// 1x votes, locked for an enactment period following a successful vote.
48	Locked1x,
49	/// 2x votes, locked for 2x enactment periods following a successful vote.
50	Locked2x,
51	/// 3x votes, locked for 4x...
52	Locked3x,
53	/// 4x votes, locked for 8x...
54	Locked4x,
55	/// 5x votes, locked for 16x...
56	Locked5x,
57	/// 6x votes, locked for 32x...
58	Locked6x,
59}
60
61impl Default for Conviction {
62	fn default() -> Self {
63		Conviction::None
64	}
65}
66
67impl From<Conviction> for u8 {
68	fn from(c: Conviction) -> u8 {
69		match c {
70			Conviction::None => 0,
71			Conviction::Locked1x => 1,
72			Conviction::Locked2x => 2,
73			Conviction::Locked3x => 3,
74			Conviction::Locked4x => 4,
75			Conviction::Locked5x => 5,
76			Conviction::Locked6x => 6,
77		}
78	}
79}
80
81impl TryFrom<u8> for Conviction {
82	type Error = ();
83	fn try_from(i: u8) -> Result<Conviction, ()> {
84		Ok(match i {
85			0 => Conviction::None,
86			1 => Conviction::Locked1x,
87			2 => Conviction::Locked2x,
88			3 => Conviction::Locked3x,
89			4 => Conviction::Locked4x,
90			5 => Conviction::Locked5x,
91			6 => Conviction::Locked6x,
92			_ => return Err(()),
93		})
94	}
95}
96
97impl Conviction {
98	/// The amount of time (in number of periods) that our conviction implies a successful voter's
99	/// balance should be locked for.
100	pub fn lock_periods(self) -> u32 {
101		match self {
102			Conviction::None => 0,
103			Conviction::Locked1x => 1,
104			Conviction::Locked2x => 2,
105			Conviction::Locked3x => 4,
106			Conviction::Locked4x => 8,
107			Conviction::Locked5x => 16,
108			Conviction::Locked6x => 32,
109		}
110	}
111
112	/// The votes of a voter of the given `balance` with our conviction.
113	pub fn votes<B: From<u8> + Zero + Copy + CheckedMul + CheckedDiv + Bounded>(
114		self,
115		capital: B,
116	) -> Delegations<B> {
117		let votes = match self {
118			Conviction::None => capital.checked_div(&10u8.into()).unwrap_or_else(Zero::zero),
119			x => capital.checked_mul(&u8::from(x).into()).unwrap_or_else(B::max_value),
120		};
121		Delegations { votes, capital }
122	}
123}
124
125impl Bounded for Conviction {
126	fn min_value() -> Self {
127		Conviction::None
128	}
129	fn max_value() -> Self {
130		Conviction::Locked6x
131	}
132}