Referenda and Conviction Voting pallets (#10195)

* Initial draft of new referendum state machine.

* Docs

* Fixes

* Fixes

* Add conviction-voting pallet

* Basic build

* Building

* Some TODOs

* Tests building

* Add missing file

* Basic lifecycle test

* Add couple of tests

* Another test

* More tests

* Fixes

* Fixes

* Formatting

* Fixes

* Tests

* Fixes

* Fixes

* More tests

* Formatting

* First few benchmarks

* First few benchmarks

* Defered queue servicing

* More testing

* Benchmarks

* Fiddly benchmark

* Final nudge benchmarks

* Formatting

* Formatting

* Finished up benchmarks

* cargo run --quiet --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_referenda --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/referenda/src/weights.rs --template=./.maintain/frame-weight-template.hbs

* Events finished

* Missing file

* No GenesisConfig for Referenda

* Formatting

* Docs

* Docs

* Docs

* Per-class conviction voting

* New test & mock utils

* More tests

* Tests

* Tests finished 🎉

* Benchmarking stuff

* Fixes

* Test harness

* Test harness

* Benchmarks for Conviction=Voting

* Benchmarking pipeline complete

* Docs

* Formatting

* Remove unneeded warning

* Fix UI tests

* cargo run --quiet --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_conviction_voting --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/conviction-voting/src/weights.rs --template=./.maintain/frame-weight-template.hbs

* Docs

* Update frame/conviction-voting/src/vote.rs

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>

* update sp-runtime version

* MEL Fixes for Referenda and Conviction Voting (#10725)

* free maxencodedlen

* more maxencodedlen

* more MEL

* more mel

* disable storage info

* More Referenda Patches (#10760)

* basic fixes

* fix benchmarking

* fix license

* prevent panic in curve math

* fmt

* bump crate versions

* Update mock.rs

Co-authored-by: Parity Bot <admin@parity.io>
Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
This commit is contained in:
Gavin Wood
2022-02-06 12:51:12 +01:00
committed by GitHub
parent 074ff19dbc
commit a6891951fb
34 changed files with 6542 additions and 106 deletions
+33 -17
View File
@@ -20,6 +20,7 @@
pub use crate::{
codec::{Codec, Decode, Encode, EncodeAsRef, EncodeLike, HasCompact, Input, Output},
scale_info::TypeInfo,
sp_std::{
fmt, marker,
prelude::{Clone, Eq, PartialEq, Vec},
@@ -33,7 +34,7 @@ pub use crate::{
TransactionPriority, WeighData, Weight, WithPostDispatchInfo,
},
};
pub use sp_runtime::{traits::Dispatchable, DispatchError};
pub use sp_runtime::{traits::Dispatchable, DispatchError, RuntimeDebug};
/// The return type of a `Dispatchable` in frame. When returned explicitly from
/// a dispatchable function it allows overriding the default `PostDispatchInfo`
@@ -60,6 +61,28 @@ pub trait Callable<T> {
// https://github.com/rust-lang/rust/issues/51331
pub type CallableCallFor<A, R> = <A as Callable<R>>::Call;
/// Origin for the System pallet.
#[derive(PartialEq, Eq, Clone, RuntimeDebug, Encode, Decode, TypeInfo)]
pub enum RawOrigin<AccountId> {
/// The system itself ordained this dispatch to happen: this is the highest privilege level.
Root,
/// It is signed by some public key and we provide the `AccountId`.
Signed(AccountId),
/// It is signed by nobody, can be either:
/// * included and agreed upon by the validators anyway,
/// * or unsigned transaction validated by a pallet.
None,
}
impl<AccountId> From<Option<AccountId>> for RawOrigin<AccountId> {
fn from(s: Option<AccountId>) -> RawOrigin<AccountId> {
match s {
Some(who) => RawOrigin::Signed(who),
None => RawOrigin::None,
}
}
}
/// A type that can be used as a parameter in a dispatchable function.
///
/// When using `decl_module` all arguments for call functions must implement this trait.
@@ -2582,21 +2605,7 @@ mod tests {
type DbWeight: Get<RuntimeDbWeight>;
}
#[derive(Clone, PartialEq, Eq, Debug, Encode, Decode, scale_info::TypeInfo)]
pub enum RawOrigin<AccountId> {
Root,
Signed(AccountId),
None,
}
impl<AccountId> From<Option<AccountId>> for RawOrigin<AccountId> {
fn from(s: Option<AccountId>) -> RawOrigin<AccountId> {
match s {
Some(who) => RawOrigin::Signed(who),
None => RawOrigin::None,
}
}
}
pub use super::super::RawOrigin;
pub type Origin<T> = RawOrigin<<T as Config>::AccountId>;
}
@@ -2638,7 +2647,7 @@ mod tests {
}
}
#[derive(scale_info::TypeInfo)]
#[derive(Eq, PartialEq, Clone, crate::RuntimeDebug, scale_info::TypeInfo)]
pub struct TraitImpl {}
impl Config for TraitImpl {}
@@ -2679,8 +2688,15 @@ mod tests {
}
}
#[derive(TypeInfo, crate::RuntimeDebug, Eq, PartialEq, Clone, Encode, Decode)]
pub struct OuterOrigin;
impl From<RawOrigin<<TraitImpl as system::Config>::AccountId>> for OuterOrigin {
fn from(_: RawOrigin<<TraitImpl as system::Config>::AccountId>) -> Self {
unimplemented!("Not required in tests!")
}
}
impl crate::traits::OriginTrait for OuterOrigin {
type Call = <TraitImpl as system::Config>::Call;
type PalletsOrigin = OuterOrigin;
+4 -2
View File
@@ -23,7 +23,7 @@ pub mod tokens;
pub use tokens::{
currency::{
Currency, LockIdentifier, LockableCurrency, NamedReservableCurrency, ReservableCurrency,
VestingSchedule,
TotalIssuanceOf, VestingSchedule,
},
fungible, fungibles,
imbalance::{Imbalance, OnUnbalanced, SignedImbalance},
@@ -90,4 +90,6 @@ mod dispatch;
pub use dispatch::{EnsureOneOf, EnsureOrigin, OriginTrait, UnfilteredDispatchable};
mod voting;
pub use voting::{CurrencyToVote, SaturatingCurrencyToVote, U128CurrencyToVote};
pub use voting::{
CurrencyToVote, PollStatus, Polling, SaturatingCurrencyToVote, U128CurrencyToVote, VoteTally,
};
@@ -17,8 +17,11 @@
//! Traits for dealing with dispatching calls and the origin from which they are dispatched.
use crate::dispatch::DispatchResultWithPostInfo;
use sp_runtime::{traits::BadOrigin, Either};
use crate::dispatch::{DispatchResultWithPostInfo, Parameter, RawOrigin};
use sp_runtime::{
traits::{BadOrigin, Member},
Either,
};
/// Some sort of check on the origin is performed by this object.
pub trait EnsureOrigin<OuterOrigin> {
@@ -56,7 +59,7 @@ pub trait OriginTrait: Sized {
type Call;
/// The caller origin, overarching type of all pallets origins.
type PalletsOrigin;
type PalletsOrigin: Parameter + Member + Into<Self> + From<RawOrigin<Self::AccountId>>;
/// The AccountId used across the system.
type AccountId;
@@ -125,7 +125,7 @@ pub mod v1 {
/// A type that can be used as a scheduler.
pub trait Anon<BlockNumber, Call, Origin> {
/// An address which can be used for removing a scheduled task.
type Address: Codec + Clone + Eq + EncodeLike + Debug;
type Address: Codec + Clone + Eq + EncodeLike + Debug + TypeInfo;
/// Schedule a dispatch to happen at the beginning of some block in the future.
///
@@ -280,7 +280,7 @@ pub mod v2 {
/// A type that can be used as a scheduler.
pub trait Anon<BlockNumber, Call, Origin> {
/// An address which can be used for removing a scheduled task.
type Address: Codec + Clone + Eq + EncodeLike + Debug;
type Address: Codec + Clone + Eq + EncodeLike + Debug + TypeInfo;
/// A means of expressing a call by the hash of its encoded data.
type Hash;
@@ -21,7 +21,10 @@ use super::{
imbalance::{Imbalance, SignedImbalance},
misc::{Balance, ExistenceRequirement, WithdrawReasons},
};
use crate::dispatch::{DispatchError, DispatchResult};
use crate::{
dispatch::{DispatchError, DispatchResult},
traits::Get,
};
use codec::MaxEncodedLen;
use sp_runtime::traits::MaybeSerializeDeserialize;
use sp_std::fmt::Debug;
@@ -200,6 +203,15 @@ pub trait Currency<AccountId> {
) -> SignedImbalance<Self::Balance, Self::PositiveImbalance>;
}
/// A non-const `Get` implementation parameterised by a `Currency` impl which provides the result
/// of `total_issuance`.
pub struct TotalIssuanceOf<C: Currency<A>, A>(sp_std::marker::PhantomData<(C, A)>);
impl<C: Currency<A>, A> Get<C::Balance> for TotalIssuanceOf<C, A> {
fn get() -> C::Balance {
C::total_issuance()
}
}
#[cfg(feature = "std")]
impl<AccountId> Currency<AccountId> for () {
type Balance = u32;
+79 -1
View File
@@ -18,7 +18,14 @@
//! Traits and associated data structures concerned with voting, and moving between tokens and
//! votes.
use sp_arithmetic::traits::{SaturatedConversion, UniqueSaturatedFrom, UniqueSaturatedInto};
use crate::dispatch::{DispatchError, Parameter};
use codec::HasCompact;
use sp_arithmetic::{
traits::{SaturatedConversion, UniqueSaturatedFrom, UniqueSaturatedInto},
Perbill,
};
use sp_runtime::traits::Member;
use sp_std::prelude::*;
/// A trait similar to `Convert` to convert values from `B` an abstract balance type
/// into u64 and back from u128. (This conversion is used in election and other places where complex
@@ -87,3 +94,74 @@ impl<B: UniqueSaturatedInto<u64> + UniqueSaturatedFrom<u128>> CurrencyToVote<B>
B::unique_saturated_from(value)
}
}
pub trait VoteTally<Votes> {
fn ayes(&self) -> Votes;
fn turnout(&self) -> Perbill;
fn approval(&self) -> Perbill;
#[cfg(feature = "runtime-benchmarks")]
fn unanimity() -> Self;
#[cfg(feature = "runtime-benchmarks")]
fn from_requirements(turnout: Perbill, approval: Perbill) -> Self;
}
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 trait Polling<Tally> {
type Index: Parameter + Member + Ord + PartialOrd + Copy + HasCompact;
type Votes: Parameter + Member + Ord + PartialOrd + Copy + HasCompact;
type Class: Parameter + Member + Ord + PartialOrd;
type Moment;
/// Provides a vec of values that `T` may take.
fn classes() -> Vec<Self::Class>;
/// `Some` if the referendum `index` can be voted on, along with the tally and class of
/// referendum.
///
/// Don't use this if you might mutate - use `try_access_poll` instead.
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>;
/// Create an ongoing majority-carries poll of given class lasting given period for the purpose
/// of benchmarking.
///
/// May return `Err` if it is impossible.
#[cfg(feature = "runtime-benchmarks")]
fn create_ongoing(class: Self::Class) -> Result<Self::Index, ()>;
/// End the given ongoing poll and return the result.
///
/// Returns `Err` if `index` is not an ongoing poll.
#[cfg(feature = "runtime-benchmarks")]
fn end_ongoing(index: Self::Index, approved: bool) -> Result<(), ()>;
/// The maximum amount of ongoing polls within any single class. By default it practically
/// unlimited (`u32::max_value()`).
#[cfg(feature = "runtime-benchmarks")]
fn max_ongoing() -> (Self::Class, u32) {
(Self::classes().into_iter().next().expect("Always one class"), u32::max_value())
}
}