use crate::dispatch::Parameter;
use alloc::{vec, vec::Vec};
use codec::{HasCompact, MaxEncodedLen};
use subsoil::arithmetic::Perbill;
use subsoil::runtime::{traits::Member, DispatchError};
pub trait VoteTally<Votes, Class> {
fn new(_: Class) -> Self;
fn ayes(&self, class: Class) -> Votes;
fn support(&self, class: Class) -> Perbill;
fn approval(&self, class: Class) -> Perbill;
#[cfg(feature = "runtime-benchmarks")]
fn unanimity(class: Class) -> Self;
#[cfg(feature = "runtime-benchmarks")]
fn rejection(class: Class) -> Self;
#[cfg(feature = "runtime-benchmarks")]
fn from_requirements(support: Perbill, approval: Perbill, class: Class) -> Self;
#[cfg(feature = "runtime-benchmarks")]
fn setup(class: Class, granularity: Perbill);
}
pub enum PollStatus<Tally, Moment, Class> {
None,
Ongoing(Tally, Class),
Completed(Moment, bool),
}
impl<Tally, Moment, Class> PollStatus<Tally, Moment, Class> {
pub fn ensure_ongoing(self) -> Option<(Tally, Class)> {
match self {
Self::Ongoing(t, c) => Some((t, c)),
_ => None,
}
}
}
pub struct ClassCountOf<P, T>(core::marker::PhantomData<(P, T)>);
impl<T, P: Polling<T>> subsoil::runtime::traits::Get<u32> for ClassCountOf<P, T> {
fn get() -> u32 {
P::classes().len() as u32
}
}
pub trait Polling<Tally> {
type Index: Parameter + Member + Ord + PartialOrd + Copy + HasCompact + MaxEncodedLen;
type Votes: Parameter + Member + Ord + PartialOrd + Copy + HasCompact + MaxEncodedLen;
type Class: Parameter + Member + Ord + PartialOrd + MaxEncodedLen;
type Moment;
fn classes() -> Vec<Self::Class>;
fn as_ongoing(index: Self::Index) -> Option<(Tally, Self::Class)>;
fn access_poll<R>(
index: Self::Index,
f: impl FnOnce(PollStatus<&mut Tally, Self::Moment, Self::Class>) -> R,
) -> R;
fn try_access_poll<R>(
index: Self::Index,
f: impl FnOnce(PollStatus<&mut Tally, Self::Moment, Self::Class>) -> Result<R, DispatchError>,
) -> Result<R, DispatchError>;
#[cfg(feature = "runtime-benchmarks")]
fn create_ongoing(class: Self::Class) -> Result<Self::Index, ()>;
#[cfg(feature = "runtime-benchmarks")]
fn end_ongoing(index: Self::Index, approved: bool) -> Result<(), ()>;
#[cfg(feature = "runtime-benchmarks")]
fn max_ongoing() -> (Self::Class, u32) {
(Self::classes().into_iter().next().expect("Always one class"), u32::max_value())
}
}
pub struct NoOpPoll<Moment>(core::marker::PhantomData<Moment>);
impl<Tally, Moment> Polling<Tally> for NoOpPoll<Moment> {
type Index = u8;
type Votes = u32;
type Class = u16;
type Moment = Moment;
fn classes() -> Vec<Self::Class> {
vec![]
}
fn as_ongoing(_index: Self::Index) -> Option<(Tally, Self::Class)> {
None
}
fn access_poll<R>(
_index: Self::Index,
f: impl FnOnce(PollStatus<&mut Tally, Self::Moment, Self::Class>) -> R,
) -> R {
f(PollStatus::None)
}
fn try_access_poll<R>(
_index: Self::Index,
f: impl FnOnce(PollStatus<&mut Tally, Self::Moment, Self::Class>) -> Result<R, DispatchError>,
) -> Result<R, DispatchError> {
f(PollStatus::None)
}
#[cfg(feature = "runtime-benchmarks")]
fn create_ongoing(_class: Self::Class) -> Result<Self::Index, ()> {
Err(())
}
#[cfg(feature = "runtime-benchmarks")]
fn end_ongoing(_index: Self::Index, _approved: bool) -> Result<(), ()> {
Err(())
}
#[cfg(feature = "runtime-benchmarks")]
fn max_ongoing() -> (Self::Class, u32) {
(0, 0)
}
}