Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

Commit

Permalink
MEL: Origin, Referenda, ConvictionVoting (paritytech#11631)
Browse files Browse the repository at this point in the history
* Referenda & CV pallets ready

* Fix build

* Add mel_bound for Voting and Casting types

* Add mel_bound on Tally

* Add mel_bound on another Tally

* Add mel_bound for pallet_collective::RawOrigin

Co-authored-by: Keith Yeung <[email protected]>
  • Loading branch information
2 people authored and DaviRain-Su committed Aug 23, 2022
1 parent 2057427 commit 8cb2280
Show file tree
Hide file tree
Showing 22 changed files with 106 additions and 39 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions frame/collective/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ use sp_runtime::{traits::Hash, RuntimeDebug};
use sp_std::{marker::PhantomData, prelude::*, result};

use frame_support::{
codec::{Decode, Encode},
codec::{Decode, Encode, MaxEncodedLen},
dispatch::{DispatchError, DispatchResultWithPostInfo, Dispatchable, PostDispatchInfo},
ensure,
traits::{
Expand Down Expand Up @@ -124,8 +124,9 @@ impl DefaultVote for MoreThanMajorityThenPrimeDefaultVote {
}

/// Origin for the collective module.
#[derive(PartialEq, Eq, Clone, RuntimeDebug, Encode, Decode, TypeInfo)]
#[derive(PartialEq, Eq, Clone, RuntimeDebug, Encode, Decode, TypeInfo, MaxEncodedLen)]
#[scale_info(skip_type_params(I))]
#[codec(mel_bound(AccountId: MaxEncodedLen))]
pub enum RawOrigin<AccountId, I> {
/// It has been condoned by a given number of members of the collective from a given total.
Members(MemberCount, MemberCount),
Expand Down
3 changes: 2 additions & 1 deletion frame/conviction-voting/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
assert_matches = "1.3.0"
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = [
codec = { package = "parity-scale-codec", version = "3.0.3", default-features = false, features = [
"derive",
"max-encoded-len",
] }
scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }
serde = { version = "1.0.136", features = ["derive"], optional = true }
Expand Down
23 changes: 18 additions & 5 deletions frame/conviction-voting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,11 @@ type ClassOf<T, I = ()> = <<T as Config<I>>::Polls as Polling<TallyOf<T, I>>>::C
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_support::{pallet_prelude::*, traits::ClassCountOf};
use frame_system::pallet_prelude::*;

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::without_storage_info]
pub struct Pallet<T, I = ()>(_);

#[pallet::config]
Expand Down Expand Up @@ -154,7 +153,7 @@ pub mod pallet {
_,
Twox64Concat,
T::AccountId,
Vec<(ClassOf<T, I>, BalanceOf<T, I>)>,
BoundedVec<(ClassOf<T, I>, BalanceOf<T, I>), ClassCountOf<T::Polls, TallyOf<T, I>>>,
ValueQuery,
>;

Expand Down Expand Up @@ -616,7 +615,15 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
ClassLocksFor::<T, I>::mutate(who, |locks| {
match locks.iter().position(|x| &x.0 == class) {
Some(i) => locks[i].1 = locks[i].1.max(amount),
None => locks.push((class.clone(), amount)),
None => {
let ok = locks.try_push((class.clone(), amount)).is_ok();
debug_assert!(
ok,
"Vec bounded by number of classes; \
all items in Vec associated with a unique class; \
qed"
);
},
}
});
T::Currency::extend_lock(CONVICTION_VOTING_ID, who, amount, WithdrawReasons::TRANSFER);
Expand All @@ -632,7 +639,13 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
let lock_needed = ClassLocksFor::<T, I>::mutate(who, |locks| {
locks.retain(|x| &x.0 != class);
if !class_lock_needed.is_zero() {
locks.push((class.clone(), class_lock_needed));
let ok = locks.try_push((class.clone(), class_lock_needed)).is_ok();
debug_assert!(
ok,
"Vec bounded by number of classes; \
all items in Vec associated with a unique class; \
qed"
);
}
locks.iter().map(|x| x.1).max().unwrap_or(Zero::zero())
});
Expand Down
1 change: 1 addition & 0 deletions frame/conviction-voting/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use crate::{AccountVote, Conviction, Vote};
MaxEncodedLen,
)]
#[scale_info(skip_type_params(Total))]
#[codec(mel_bound(Votes: MaxEncodedLen))]
pub struct Tally<Votes: Clone + PartialEq + Eq + Debug + TypeInfo + Codec, Total> {
/// The number of aye votes, expressed in terms of post-conviction lock-vote.
pub ayes: Votes,
Expand Down
5 changes: 5 additions & 0 deletions frame/conviction-voting/src/vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ pub struct Delegating<Balance, AccountId, BlockNumber> {
/// Information concerning the direct vote-casting of some voting power.
#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
#[scale_info(skip_type_params(MaxVotes))]
#[codec(mel_bound(Balance: MaxEncodedLen, BlockNumber: MaxEncodedLen, PollIndex: MaxEncodedLen))]
pub struct Casting<Balance, BlockNumber, PollIndex, MaxVotes>
where
MaxVotes: Get<u32>,
Expand All @@ -177,6 +178,10 @@ where
/// An indicator for what an account is doing; it can either be delegating or voting.
#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
#[scale_info(skip_type_params(MaxVotes))]
#[codec(mel_bound(
Balance: MaxEncodedLen, AccountId: MaxEncodedLen, BlockNumber: MaxEncodedLen,
PollIndex: MaxEncodedLen,
))]
pub enum Voting<Balance, AccountId, BlockNumber, PollIndex, MaxVotes>
where
MaxVotes: Get<u32>,
Expand Down
1 change: 1 addition & 0 deletions frame/ranked-collective/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ pub type Votes = u32;
MaxEncodedLen,
)]
#[scale_info(skip_type_params(M))]
#[codec(mel_bound())]
pub struct Tally<M: GetMaxVoters> {
bare_ayes: MemberIndex,
ayes: Votes,
Expand Down
2 changes: 1 addition & 1 deletion frame/referenda/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
assert_matches = { version = "1.5", optional = true }
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = [
codec = { package = "parity-scale-codec", version = "3.0.3", default-features = false, features = [
"derive",
] }
scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }
Expand Down
4 changes: 2 additions & 2 deletions frame/referenda/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ pub mod pallet {

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::without_storage_info]
pub struct Pallet<T, I = ()>(_);

#[pallet::config]
Expand Down Expand Up @@ -159,7 +158,8 @@ pub mod pallet {
+ Codec
+ Eq
+ Debug
+ TypeInfo;
+ TypeInfo
+ MaxEncodedLen;

// Constants
/// The minimum amount to be used as a deposit for a public referendum proposal.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ pub fn expand_outer_origin(

#[derive(
Clone, PartialEq, Eq, #scrate::RuntimeDebug, #scrate::codec::Encode,
#scrate::codec::Decode, #scrate::scale_info::TypeInfo,
#scrate::codec::Decode, #scrate::scale_info::TypeInfo, #scrate::codec::MaxEncodedLen,
)]
#[allow(non_camel_case_types)]
pub enum OriginCaller {
Expand Down
10 changes: 7 additions & 3 deletions frame/support/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
//! generating values representing lazy module function calls.
pub use crate::{
codec::{Codec, Decode, Encode, EncodeAsRef, EncodeLike, HasCompact, Input, Output},
codec::{
Codec, Decode, Encode, EncodeAsRef, EncodeLike, HasCompact, Input, MaxEncodedLen, Output,
},
scale_info::TypeInfo,
sp_std::{
fmt, marker,
Expand Down Expand Up @@ -63,7 +65,7 @@ pub trait Callable<T> {
pub type CallableCallFor<A, R> = <A as Callable<R>>::Call;

/// Origin for the System pallet.
#[derive(PartialEq, Eq, Clone, RuntimeDebug, Encode, Decode, TypeInfo)]
#[derive(PartialEq, Eq, Clone, RuntimeDebug, Encode, Decode, TypeInfo, MaxEncodedLen)]
pub enum RawOrigin<AccountId> {
/// The system itself ordained this dispatch to happen: this is the highest privilege level.
Root,
Expand Down Expand Up @@ -2698,7 +2700,9 @@ mod tests {
}
}

#[derive(TypeInfo, crate::RuntimeDebug, Eq, PartialEq, Clone, Encode, Decode)]
#[derive(
TypeInfo, crate::RuntimeDebug, Eq, PartialEq, Clone, Encode, Decode, MaxEncodedLen,
)]
pub struct OuterOrigin;

impl From<RawOrigin<<TraitImpl as system::Config>::AccountId>> for OuterOrigin {
Expand Down
3 changes: 2 additions & 1 deletion frame/support/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,6 @@ pub use dispatch::{

mod voting;
pub use voting::{
CurrencyToVote, PollStatus, Polling, SaturatingCurrencyToVote, U128CurrencyToVote, VoteTally,
ClassCountOf, CurrencyToVote, PollStatus, Polling, SaturatingCurrencyToVote,
U128CurrencyToVote, VoteTally,
};
7 changes: 6 additions & 1 deletion frame/support/src/traits/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
//! Traits for dealing with dispatching calls and the origin from which they are dispatched.
use crate::dispatch::{DispatchResultWithPostInfo, Parameter, RawOrigin};
use codec::MaxEncodedLen;
use sp_runtime::{
traits::{BadOrigin, Member, Morph, TryMorph},
Either,
Expand Down Expand Up @@ -258,7 +259,11 @@ pub trait OriginTrait: Sized {
type Call;

/// The caller origin, overarching type of all pallets origins.
type PalletsOrigin: Parameter + Member + Into<Self> + From<RawOrigin<Self::AccountId>>;
type PalletsOrigin: Parameter
+ Member
+ Into<Self>
+ From<RawOrigin<Self::AccountId>>
+ MaxEncodedLen;

/// The AccountId used across the system.
type AccountId;
Expand Down
8 changes: 4 additions & 4 deletions frame/support/src/traits/schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,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 + TypeInfo;
type Address: Codec + Clone + Eq + EncodeLike + Debug + TypeInfo + MaxEncodedLen;

/// Schedule a dispatch to happen at the beginning of some block in the future.
///
Expand Down Expand Up @@ -179,7 +179,7 @@ pub mod v1 {
/// A type that can be used as a scheduler.
pub trait Named<BlockNumber, Call, Origin> {
/// An address which can be used for removing a scheduled task.
type Address: Codec + Clone + Eq + EncodeLike + sp_std::fmt::Debug;
type Address: Codec + Clone + Eq + EncodeLike + sp_std::fmt::Debug + MaxEncodedLen;

/// Schedule a dispatch to happen at the beginning of some block in the future.
///
Expand Down Expand Up @@ -289,7 +289,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 + TypeInfo;
type Address: Codec + Clone + Eq + EncodeLike + Debug + TypeInfo + MaxEncodedLen;
/// A means of expressing a call by the hash of its encoded data.
type Hash;

Expand Down Expand Up @@ -336,7 +336,7 @@ pub mod v2 {
/// A type that can be used as a scheduler.
pub trait Named<BlockNumber, Call, Origin> {
/// An address which can be used for removing a scheduled task.
type Address: Codec + Clone + Eq + EncodeLike + sp_std::fmt::Debug;
type Address: Codec + Clone + Eq + EncodeLike + sp_std::fmt::Debug + MaxEncodedLen;
/// A means of expressing a call by the hash of its encoded data.
type Hash;

Expand Down
7 changes: 7 additions & 0 deletions frame/support/src/traits/voting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ impl<Tally, Moment, Class> PollStatus<Tally, Moment, Class> {
}
}

pub struct ClassCountOf<P, T>(sp_std::marker::PhantomData<(P, T)>);
impl<T, P: Polling<T>> sp_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;
Expand Down
17 changes: 13 additions & 4 deletions frame/support/test/tests/construct_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#![recursion_limit = "128"]

use codec::MaxEncodedLen;
use frame_support::traits::{CrateVersion, PalletInfo as _};
use scale_info::TypeInfo;
use sp_core::{sr25519, H256};
Expand Down Expand Up @@ -55,7 +56,9 @@ mod module1 {
}
}

#[derive(Clone, PartialEq, Eq, Debug, codec::Encode, codec::Decode, TypeInfo)]
#[derive(
Clone, PartialEq, Eq, Debug, codec::Encode, codec::Decode, TypeInfo, MaxEncodedLen,
)]
pub struct Origin<T, I: Instance = DefaultInstance>(pub core::marker::PhantomData<(T, I)>);

frame_support::decl_event! {
Expand Down Expand Up @@ -97,7 +100,9 @@ mod module2 {
}
}

#[derive(Clone, PartialEq, Eq, Debug, codec::Encode, codec::Decode, TypeInfo)]
#[derive(
Clone, PartialEq, Eq, Debug, codec::Encode, codec::Decode, TypeInfo, MaxEncodedLen,
)]
pub struct Origin;

frame_support::decl_event! {
Expand Down Expand Up @@ -140,7 +145,9 @@ mod nested {
}
}

#[derive(Clone, PartialEq, Eq, Debug, codec::Encode, codec::Decode, TypeInfo)]
#[derive(
Clone, PartialEq, Eq, Debug, codec::Encode, codec::Decode, TypeInfo, MaxEncodedLen,
)]
pub struct Origin;

frame_support::decl_event! {
Expand Down Expand Up @@ -196,7 +203,9 @@ pub mod module3 {
}
}

#[derive(Clone, PartialEq, Eq, Debug, codec::Encode, codec::Decode, TypeInfo)]
#[derive(
Clone, PartialEq, Eq, Debug, codec::Encode, codec::Decode, TypeInfo, MaxEncodedLen,
)]
pub struct Origin<T>(pub core::marker::PhantomData<T>);

frame_support::decl_event! {
Expand Down
10 changes: 7 additions & 3 deletions frame/support/test/tests/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

#![recursion_limit = "128"]

use codec::{Codec, Decode, Encode, EncodeLike};
use codec::{Codec, Decode, Encode, EncodeLike, MaxEncodedLen};
use frame_support::{
inherent::{InherentData, InherentIdentifier, MakeFatalError, ProvideInherent},
metadata::{
Expand Down Expand Up @@ -108,7 +108,9 @@ mod module1 {
}
}

#[derive(PartialEq, Eq, Clone, sp_runtime::RuntimeDebug, Encode, Decode, TypeInfo)]
#[derive(
PartialEq, Eq, Clone, sp_runtime::RuntimeDebug, Encode, Decode, TypeInfo, MaxEncodedLen,
)]
pub enum Origin<T: Config<I>, I>
where
T::BlockNumber: From<u32>,
Expand Down Expand Up @@ -181,7 +183,9 @@ mod module2 {
}
}

#[derive(PartialEq, Eq, Clone, sp_runtime::RuntimeDebug, Encode, Decode, TypeInfo)]
#[derive(
PartialEq, Eq, Clone, sp_runtime::RuntimeDebug, Encode, Decode, TypeInfo, MaxEncodedLen,
)]
pub enum Origin<T: Config<I>, I = DefaultInstance> {
Members(u32),
_Phantom(std::marker::PhantomData<(T, I)>),
Expand Down
Loading

0 comments on commit 8cb2280

Please sign in to comment.