Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate pallet-ranked-collective to use umbrella crate #7235

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions Cargo.lock

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

25 changes: 4 additions & 21 deletions substrate/frame/ranked-collective/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,22 @@ targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { features = ["derive"], workspace = true }
frame-benchmarking = { optional = true, workspace = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
frame = { workspace = true, features = ["runtime"]}
impl-trait-for-tuples = { workspace = true }
log = { workspace = true }
scale-info = { features = ["derive"], workspace = true }
sp-arithmetic = { workspace = true }
sp-core = { workspace = true }
sp-io = { workspace = true }
sp-runtime = { workspace = true }

[features]
default = ["std"]
std = [
"codec/std",
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"log/std",
"scale-info/std",
"sp-arithmetic/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"frame/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"frame/runtime-benchmarks",
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"sp-runtime/try-runtime",
"frame/try-runtime",
]
10 changes: 3 additions & 7 deletions substrate/frame/ranked-collective/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,9 @@ use super::*;
#[allow(unused_imports)]
use crate::Pallet as RankedCollective;
use alloc::vec::Vec;
use frame_benchmarking::{
v1::{account, BenchmarkError},
v2::*,
};
use frame::benchmarking::prelude::{RawOrigin as SystemOrigin, *};


use frame_support::{assert_err, assert_ok, traits::NoOpPoll};
use frame_system::RawOrigin as SystemOrigin;

const SEED: u32 = 0;

Expand Down Expand Up @@ -58,7 +54,7 @@ fn make_member<T: Config<I>, I: 'static>(rank: Rank) -> T::AccountId {
}

#[instance_benchmarks(
where <<T as pallet::Config<I>>::Polls as frame_support::traits::Polling<Tally<T, I, pallet::Pallet<T, I>>>>::Index: From<u8>
where <<T as pallet::Config<I>>::Polls as Polling<Tally<T, I, pallet::Pallet<T, I>>>>::Index: From<u8>
)]
mod benchmarks {
use super::*;
Expand Down
37 changes: 12 additions & 25 deletions substrate/frame/ranked-collective/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,7 @@ extern crate alloc;
use codec::{Decode, Encode, MaxEncodedLen};
use core::marker::PhantomData;
use scale_info::TypeInfo;
use sp_arithmetic::traits::Saturating;
use sp_runtime::{
traits::{Convert, StaticLookup},
ArithmeticError::Overflow,
DispatchError, Perbill, RuntimeDebug,
};

use frame_support::{
dispatch::{DispatchResultWithPostInfo, PostDispatchInfo},
ensure, impl_ensure_origin_with_arg_ignoring_arg,
traits::{
EnsureOrigin, EnsureOriginWithArg, PollStatus, Polling, RankedMembers,
RankedMembersSwapHandler, VoteTally,
},
CloneNoBound, EqNoBound, PartialEqNoBound, RuntimeDebugNoBound,
};
use frame::prelude::*;

#[cfg(test)]
mod tests;
Expand All @@ -72,6 +57,11 @@ pub mod weights;
pub use pallet::*;
pub use weights::WeightInfo;

use frame::runtime::prelude::storage::KeyLenOf;

#[cfg(feature = "try-runtime")]
use frame::try_runtime::TryRuntimeError;

/// A number of members.
pub type MemberIndex = u32;

Expand Down Expand Up @@ -376,12 +366,9 @@ pub trait BenchmarkSetup<AccountId> {
fn ensure_member(acc: &AccountId);
}

#[frame_support::pallet]
#[frame::pallet]
pub mod pallet {
use super::*;
use frame_support::{pallet_prelude::*, storage::KeyLenOf};
use frame_system::pallet_prelude::*;
use sp_runtime::traits::MaybeConvert;

#[pallet::pallet]
pub struct Pallet<T, I = ()>(PhantomData<(T, I)>);
Expand Down Expand Up @@ -859,9 +846,9 @@ pub mod pallet {
/// Determine the rank of the account behind the `Signed` origin `o`, `None` if the account
/// is unknown to this collective or `o` is not `Signed`.
pub fn as_rank(
o: &<T::RuntimeOrigin as frame_support::traits::OriginTrait>::PalletsOrigin,
o: &<T::RuntimeOrigin as OriginTrait>::PalletsOrigin,
) -> Option<u16> {
use frame_support::traits::CallerTrait;
use frame::traits::CallerTrait;
o.as_signed().and_then(Self::rank_of)
}

Expand All @@ -878,7 +865,7 @@ pub mod pallet {
#[cfg(any(feature = "try-runtime", test))]
impl<T: Config<I>, I: 'static> Pallet<T, I> {
/// Ensure the correctness of the state of this pallet.
pub fn do_try_state() -> Result<(), sp_runtime::TryRuntimeError> {
pub fn do_try_state() -> Result<(), TryRuntimeError> {
Self::try_state_members()?;
Self::try_state_index()?;

Expand All @@ -892,7 +879,7 @@ pub mod pallet {
/// [`Rank`] in Members should be in [`MemberCount`]
/// [`Sum`] of [`MemberCount`] index should be the same as the sum of all the index attained
/// for rank possessed by [`Members`]
fn try_state_members() -> Result<(), sp_runtime::TryRuntimeError> {
fn try_state_members() -> Result<(),TryRuntimeError> {
MemberCount::<T, I>::iter().try_for_each(|(_, member_index)| -> DispatchResult {
let total_members = Members::<T, I>::iter().count();
ensure!(
Expand Down Expand Up @@ -929,7 +916,7 @@ pub mod pallet {
/// [`Rank`] in [`IdToIndex`] should be the same as the the [`Rank`] in [`IndexToId`]
/// [`Rank`] of the member [`who`] in [`IdToIndex`] should be the same as the [`Rank`] of
/// the member [`who`] in [`Members`]
fn try_state_index() -> Result<(), sp_runtime::TryRuntimeError> {
fn try_state_index() -> Result<(), TryRuntimeError> {
IdToIndex::<T, I>::iter().try_for_each(
|(rank, who, member_index)| -> DispatchResult {
let who_from_index = IndexToId::<T, I>::get(rank, member_index).unwrap();
Expand Down
21 changes: 7 additions & 14 deletions substrate/frame/ranked-collective/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,16 @@

use std::collections::BTreeMap;

use frame_support::{
assert_noop, assert_ok, derive_impl, parameter_types,
traits::{ConstU16, EitherOf, MapSuccess, Polling},
};
use sp_core::Get;
use sp_runtime::{
traits::{BadOrigin, MaybeConvert, ReduceBy, ReplaceWithDefault},
BuildStorage,
};
use frame::testing_prelude::*;


use super::*;
use crate as pallet_ranked_collective;

type Block = frame_system::mocking::MockBlock<Test>;
type Block = MockBlock<Test>;
type Class = Rank;

frame_support::construct_runtime!(
construct_runtime!(
pub enum Test
{
System: frame_system,
Expand Down Expand Up @@ -202,9 +195,9 @@ impl Default for ExtBuilder {
}

impl ExtBuilder {
pub fn build(self) -> sp_io::TestExternalities {
pub fn build(self) -> TestState {
let t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
let mut ext = sp_io::TestExternalities::new(t);
let mut ext = TestState::new(t);
ext.execute_with(|| System::set_block_number(1));
ext
}
Expand Down Expand Up @@ -497,7 +490,7 @@ fn ensure_ranked_works() {
assert_ok!(Club::promote_member(RuntimeOrigin::root(), 3));
assert_ok!(Club::promote_member(RuntimeOrigin::root(), 3));

use frame_support::traits::OriginTrait;
use frame::traits::OriginTrait;
type Rank1 = EnsureRanked<Test, (), 1>;
type Rank2 = EnsureRanked<Test, (), 2>;
type Rank3 = EnsureRanked<Test, (), 3>;
Expand Down
2 changes: 1 addition & 1 deletion substrate/frame/ranked-collective/src/weights.rs

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

35 changes: 26 additions & 9 deletions substrate/frame/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,19 @@ pub mod prelude {

/// Dispatch types from `frame-support`, other fundamental traits
#[doc(no_inline)]
pub use frame_support::dispatch::{GetDispatchInfo, PostDispatchInfo};
pub use frame_support::traits::{
Contains, EitherOf, EstimateNextSessionRotation, Everything, IsSubType, MapSuccess,
NoOpPoll, OnRuntimeUpgrade, OneSessionHandler, RankedMembers, RankedMembersSwapHandler,
VariantCount, VariantCountOf,
pub use frame_support::dispatch::{
DispatchResultWithPostInfo, GetDispatchInfo, PostDispatchInfo,
};

pub use frame_support::traits::{CallerTrait, OriginTrait};

pub use frame_support::{
defensive, defensive_assert, ensure, impl_ensure_origin_with_arg_ignoring_arg,
traits::{
Contains, EitherOf, EnsureOrigin, EnsureOriginWithArg, EstimateNextSessionRotation,
IsSubType, MapSuccess, NoOpPoll, OnRuntimeUpgrade, OneSessionHandler, PollStatus,
Polling, RankedMembers, RankedMembersSwapHandler, VoteTally,
},
};

/// Pallet prelude of `frame-system`.
Expand All @@ -231,18 +239,26 @@ pub mod prelude {

/// Runtime traits
#[doc(no_inline)]
pub use sp_runtime::traits::{
BlockNumberProvider, Bounded, Convert, DispatchInfoOf, Dispatchable, ReduceBy,
ReplaceWithDefault, SaturatedConversion, Saturating, StaticLookup, TrailingZeroInput,
pub use sp_runtime::{
traits::{
BadOrigin, BlockNumberProvider, Bounded, Convert, DispatchInfoOf, Dispatchable,
MaybeConvert, ReduceBy, ReplaceWithDefault, SaturatedConversion, Saturating,
StaticLookup, TrailingZeroInput,
},
Perbill, RuntimeDebug,
};

// Error variant
pub use sp_runtime::ArithmeticError::Overflow;

/// Bounded storage related types.
pub use sp_runtime::{BoundedSlice, BoundedVec};

/// Other error/result types for runtime
#[doc(no_inline)]
pub use sp_runtime::{
BoundToRuntimeAppPublic, DispatchErrorWithPostInfo, DispatchResultWithInfo, TokenError,
BoundToRuntimeAppPublic, DispatchError, DispatchErrorWithPostInfo, DispatchResultWithInfo,
TokenError,
};
}

Expand Down Expand Up @@ -338,6 +354,7 @@ pub mod testing_prelude {

/// Commonly used runtime traits for testing.
pub use sp_runtime::{traits::BadOrigin, StateVersion};

}

/// All of the types and tools needed to build FRAME-based runtimes.
Expand Down
Loading