use sp_std::{prelude::*, result::Result, convert::TryFrom};
use codec::{Encode, EncodeLike, Decode, Output, Input};
use sp_runtime::{RuntimeDebug, traits::{Saturating, Zero}};
use crate::{Conviction, ReferendumIndex, Delegations};
#[derive(Copy, Clone, Eq, PartialEq, Default, RuntimeDebug)]
pub struct Vote {
pub aye: bool,
pub conviction: Conviction,
}
impl Encode for Vote {
fn encode_to<T: Output>(&self, output: &mut T) {
output.push_byte(u8::from(self.conviction) | if self.aye { 0b1000_0000 } else { 0 });
}
}
impl EncodeLike for Vote {}
impl Decode for Vote {
fn decode<I: Input>(input: &mut I) -> Result<Self, codec::Error> {
let b = input.read_byte()?;
Ok(Vote {
aye: (b & 0b1000_0000) == 0b1000_0000,
conviction: Conviction::try_from(b & 0b0111_1111)
.map_err(|_| codec::Error::from("Invalid conviction"))?,
})
}
}
#[derive(Encode, Decode, Copy, Clone, Eq, PartialEq, RuntimeDebug)]
pub enum AccountVote<Balance> {
Standard { vote: Vote, balance: Balance },
Split { aye: Balance, nay: Balance },
}
impl<Balance: Saturating> AccountVote<Balance> {
pub fn locked_if(self, approved: bool) -> Option<(u32, Balance)> {
match self {
AccountVote::Standard { vote, balance } if vote.aye == approved =>
Some((vote.conviction.lock_periods(), balance)),
_ => None,
}
}
pub fn balance(self) -> Balance {
match self {
AccountVote::Standard { balance, .. } => balance,
AccountVote::Split { aye, nay } => aye.saturating_add(nay),
}
}
pub fn as_standard(self) -> Option<bool> {
match self {
AccountVote::Standard { vote, .. } => Some(vote.aye),
_ => None,
}
}
}
#[derive(Encode, Decode, Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, RuntimeDebug)]
pub struct PriorLock<BlockNumber, Balance>(BlockNumber, Balance);
impl<BlockNumber: Ord + Copy + Zero, Balance: Ord + Copy + Zero> PriorLock<BlockNumber, Balance> {
pub fn accumulate(&mut self, until: BlockNumber, amount: Balance) {
self.0 = self.0.max(until);
self.1 = self.1.max(amount);
}
pub fn locked(&self) -> Balance {
self.1
}
pub fn rejig(&mut self, now: BlockNumber) {
if now >= self.0 {
self.0 = Zero::zero();
self.1 = Zero::zero();
}
}
}
#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug)]
pub enum Voting<Balance, AccountId, BlockNumber> {
Direct {
votes: Vec<(ReferendumIndex, AccountVote<Balance>)>,
delegations: Delegations<Balance>,
prior: PriorLock<BlockNumber, Balance>,
},
Delegating {
balance: Balance,
target: AccountId,
conviction: Conviction,
delegations: Delegations<Balance>,
prior: PriorLock<BlockNumber, Balance>,
},
}
impl<Balance: Default, AccountId, BlockNumber: Zero> Default for Voting<Balance, AccountId, BlockNumber> {
fn default() -> Self {
Voting::Direct {
votes: Vec::new(),
delegations: Default::default(),
prior: PriorLock(Zero::zero(), Default::default()),
}
}
}
impl<
Balance: Saturating + Ord + Zero + Copy,
BlockNumber: Ord + Copy + Zero,
AccountId,
> Voting<Balance, AccountId, BlockNumber> {
pub fn rejig(&mut self, now: BlockNumber) {
match self {
Voting::Direct { prior, .. } => prior,
Voting::Delegating { prior, .. } => prior,
}.rejig(now);
}
pub fn locked_balance(&self) -> Balance {
match self {
Voting::Direct { votes, prior, .. } => votes.iter()
.map(|i| i.1.balance())
.fold(prior.locked(), |a, i| a.max(i)),
Voting::Delegating { balance, .. } => *balance,
}
}
pub fn set_common(&mut self,
delegations: Delegations<Balance>,
prior: PriorLock<BlockNumber, Balance>
) {
let (d, p) = match self {
Voting::Direct { ref mut delegations, ref mut prior, .. } => (delegations, prior),
Voting::Delegating { ref mut delegations, ref mut prior, .. } => (delegations, prior),
};
*d = delegations;
*p = prior;
}
}