referrerpolicy=no-referrer-when-downgrade

pallet_conviction_voting/
vote.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 vote datatype.
19
20use crate::{Conviction, Delegations};
21use codec::{Decode, DecodeWithMemTracking, Encode, EncodeLike, Input, MaxEncodedLen, Output};
22use frame_support::{pallet_prelude::Get, BoundedVec};
23use scale_info::TypeInfo;
24use sp_runtime::{
25	traits::{Saturating, Zero},
26	Debug,
27};
28
29/// A number of lock periods, plus a vote, one way or the other.
30#[derive(DecodeWithMemTracking, Copy, Clone, Eq, PartialEq, Default, Debug, MaxEncodedLen)]
31pub struct Vote {
32	pub aye: bool,
33	pub conviction: Conviction,
34}
35
36impl Encode for Vote {
37	fn encode_to<T: Output + ?Sized>(&self, output: &mut T) {
38		output.push_byte(u8::from(self.conviction) | if self.aye { 0b1000_0000 } else { 0 });
39	}
40}
41
42impl EncodeLike for Vote {}
43
44impl Decode for Vote {
45	fn decode<I: Input>(input: &mut I) -> Result<Self, codec::Error> {
46		let b = input.read_byte()?;
47		Ok(Vote {
48			aye: (b & 0b1000_0000) == 0b1000_0000,
49			conviction: Conviction::try_from(b & 0b0111_1111)
50				.map_err(|_| codec::Error::from("Invalid conviction"))?,
51		})
52	}
53}
54
55impl TypeInfo for Vote {
56	type Identity = Self;
57
58	fn type_info() -> scale_info::Type {
59		scale_info::Type::builder()
60			.path(scale_info::Path::new("Vote", module_path!()))
61			.composite(
62				scale_info::build::Fields::unnamed()
63					.field(|f| f.ty::<u8>().docs(&["Raw vote byte, encodes aye + conviction"])),
64			)
65	}
66}
67
68/// A vote for a referendum of a particular account.
69#[derive(
70	Encode,
71	Decode,
72	DecodeWithMemTracking,
73	Copy,
74	Clone,
75	Eq,
76	PartialEq,
77	Debug,
78	TypeInfo,
79	MaxEncodedLen,
80)]
81pub enum AccountVote<Balance> {
82	/// A standard vote, one-way (approve or reject) with a given amount of conviction.
83	Standard { vote: Vote, balance: Balance },
84	/// A split vote with balances given for both ways, and with no conviction, useful for
85	/// parachains when voting.
86	Split { aye: Balance, nay: Balance },
87	/// A split vote with balances given for both ways as well as abstentions, and with no
88	/// conviction, useful for parachains when voting, other off-chain aggregate accounts and
89	/// individuals who wish to abstain.
90	SplitAbstain { aye: Balance, nay: Balance, abstain: Balance },
91}
92
93/// Present the conditions under which an account's Funds are locked after a voting action.
94#[derive(Copy, Clone, Eq, PartialEq, Debug)]
95pub enum LockedIf {
96	/// Lock the funds if the outcome of the referendum matches the voting behavior of the user.
97	///
98	/// `true` means they voted `aye` and `false` means `nay`.
99	Status(bool),
100	/// Always lock the funds.
101	Always,
102}
103
104impl<Balance: Saturating> AccountVote<Balance> {
105	/// Returns `Some` of the lock periods that the account is locked for, assuming that the
106	/// referendum passed if `approved` is `true`.
107	pub fn locked_if(self, approved: LockedIf) -> Option<(u32, Balance)> {
108		// winning side: can only be removed after the lock period ends.
109		match (self, approved) {
110			// If the vote has no conviction, always return None
111			(AccountVote::Standard { vote: Vote { conviction: Conviction::None, .. }, .. }, _) => {
112				None
113			},
114
115			// For Standard votes, check the approval condition
116			(AccountVote::Standard { vote, balance }, LockedIf::Status(is_approved))
117				if vote.aye == is_approved =>
118			{
119				Some((vote.conviction.lock_periods(), balance))
120			},
121
122			// If LockedIf::Always, return the lock period regardless of the vote
123			(AccountVote::Standard { vote, balance }, LockedIf::Always) => {
124				Some((vote.conviction.lock_periods(), balance))
125			},
126
127			// All other cases return None
128			_ => None,
129		}
130	}
131
132	/// The total balance involved in this vote.
133	pub fn balance(self) -> Balance {
134		match self {
135			AccountVote::Standard { balance, .. } => balance,
136			AccountVote::Split { aye, nay } => aye.saturating_add(nay),
137			AccountVote::SplitAbstain { aye, nay, abstain } => {
138				aye.saturating_add(nay).saturating_add(abstain)
139			},
140		}
141	}
142
143	/// Returns `Some` with whether the vote is an aye vote if it is standard, otherwise `None` if
144	/// it is split.
145	pub fn as_standard(self) -> Option<bool> {
146		match self {
147			AccountVote::Standard { vote, .. } => Some(vote.aye),
148			_ => None,
149		}
150	}
151}
152
153/// A "prior" lock, i.e. a lock for some now-forgotten reason.
154#[derive(
155	Encode,
156	Decode,
157	DecodeWithMemTracking,
158	Default,
159	Copy,
160	Clone,
161	Eq,
162	PartialEq,
163	Ord,
164	PartialOrd,
165	Debug,
166	TypeInfo,
167	MaxEncodedLen,
168)]
169pub struct PriorLock<BlockNumber, Balance>(BlockNumber, Balance);
170
171impl<BlockNumber: Ord + Copy + Zero, Balance: Ord + Copy + Zero> PriorLock<BlockNumber, Balance> {
172	/// Accumulates an additional lock.
173	pub fn accumulate(&mut self, until: BlockNumber, amount: Balance) {
174		self.0 = self.0.max(until);
175		self.1 = self.1.max(amount);
176	}
177
178	pub fn locked(&self) -> Balance {
179		self.1
180	}
181
182	pub fn rejig(&mut self, now: BlockNumber) {
183		if now >= self.0 {
184			self.0 = Zero::zero();
185			self.1 = Zero::zero();
186		}
187	}
188}
189
190/// Information concerning the delegation of some voting power.
191#[derive(
192	Encode, Decode, DecodeWithMemTracking, Clone, Eq, PartialEq, Debug, TypeInfo, MaxEncodedLen,
193)]
194pub struct Delegating<Balance, AccountId, BlockNumber> {
195	/// The amount of balance delegated.
196	pub balance: Balance,
197	/// The account to which the voting power is delegated.
198	pub target: AccountId,
199	/// The conviction with which the voting power is delegated. When this gets undelegated, the
200	/// relevant lock begins.
201	pub conviction: Conviction,
202	/// The total amount of delegations that this account has received, post-conviction-weighting.
203	pub delegations: Delegations<Balance>,
204	/// Any pre-existing locks from past voting/delegating activity.
205	pub prior: PriorLock<BlockNumber, Balance>,
206}
207
208/// Information concerning the direct vote-casting of some voting power.
209#[derive(
210	Encode, Decode, DecodeWithMemTracking, Clone, Eq, PartialEq, Debug, TypeInfo, MaxEncodedLen,
211)]
212#[scale_info(skip_type_params(MaxVotes))]
213#[codec(mel_bound(Balance: MaxEncodedLen, BlockNumber: MaxEncodedLen, PollIndex: MaxEncodedLen))]
214pub struct Casting<Balance, BlockNumber, PollIndex, MaxVotes>
215where
216	MaxVotes: Get<u32>,
217{
218	/// The current votes of the account.
219	pub votes: BoundedVec<(PollIndex, AccountVote<Balance>), MaxVotes>,
220	/// The total amount of delegations that this account has received, post-conviction-weighting.
221	pub delegations: Delegations<Balance>,
222	/// Any pre-existing locks from past voting/delegating activity.
223	pub prior: PriorLock<BlockNumber, Balance>,
224}
225
226/// An indicator for what an account is doing; it can either be delegating or voting.
227#[derive(
228	Encode, Decode, DecodeWithMemTracking, Clone, Eq, PartialEq, Debug, TypeInfo, MaxEncodedLen,
229)]
230#[scale_info(skip_type_params(MaxVotes))]
231#[codec(mel_bound(
232	Balance: MaxEncodedLen, AccountId: MaxEncodedLen, BlockNumber: MaxEncodedLen,
233	PollIndex: MaxEncodedLen,
234))]
235pub enum Voting<Balance, AccountId, BlockNumber, PollIndex, MaxVotes>
236where
237	MaxVotes: Get<u32>,
238{
239	/// The account is voting directly.
240	Casting(Casting<Balance, BlockNumber, PollIndex, MaxVotes>),
241	/// The account is delegating `balance` of its balance to a `target` account with `conviction`.
242	Delegating(Delegating<Balance, AccountId, BlockNumber>),
243}
244
245impl<Balance: Default, AccountId, BlockNumber: Zero, PollIndex, MaxVotes> Default
246	for Voting<Balance, AccountId, BlockNumber, PollIndex, MaxVotes>
247where
248	MaxVotes: Get<u32>,
249{
250	fn default() -> Self {
251		Voting::Casting(Casting {
252			votes: Default::default(),
253			delegations: Default::default(),
254			prior: PriorLock(Zero::zero(), Default::default()),
255		})
256	}
257}
258
259impl<Balance, AccountId, BlockNumber, PollIndex, MaxVotes> AsMut<PriorLock<BlockNumber, Balance>>
260	for Voting<Balance, AccountId, BlockNumber, PollIndex, MaxVotes>
261where
262	MaxVotes: Get<u32>,
263{
264	fn as_mut(&mut self) -> &mut PriorLock<BlockNumber, Balance> {
265		match self {
266			Voting::Casting(Casting { prior, .. }) => prior,
267			Voting::Delegating(Delegating { prior, .. }) => prior,
268		}
269	}
270}
271
272impl<
273		Balance: Saturating + Ord + Zero + Copy,
274		BlockNumber: Ord + Copy + Zero,
275		AccountId,
276		PollIndex,
277		MaxVotes,
278	> Voting<Balance, AccountId, BlockNumber, PollIndex, MaxVotes>
279where
280	MaxVotes: Get<u32>,
281{
282	pub fn rejig(&mut self, now: BlockNumber) {
283		AsMut::<PriorLock<BlockNumber, Balance>>::as_mut(self).rejig(now);
284	}
285
286	/// The amount of this account's balance that must currently be locked due to voting.
287	pub fn locked_balance(&self) -> Balance {
288		match self {
289			Voting::Casting(Casting { votes, prior, .. }) => {
290				votes.iter().map(|i| i.1.balance()).fold(prior.locked(), |a, i| a.max(i))
291			},
292			Voting::Delegating(Delegating { balance, prior, .. }) => *balance.max(&prior.locked()),
293		}
294	}
295
296	pub fn set_common(
297		&mut self,
298		delegations: Delegations<Balance>,
299		prior: PriorLock<BlockNumber, Balance>,
300	) {
301		let (d, p) = match self {
302			Voting::Casting(Casting { ref mut delegations, ref mut prior, .. }) => {
303				(delegations, prior)
304			},
305			Voting::Delegating(Delegating { ref mut delegations, ref mut prior, .. }) => {
306				(delegations, prior)
307			},
308		};
309		*d = delegations;
310		*p = prior;
311	}
312}