Skip to content

Commit

Permalink
Migrating salary pallet to use umbrella crate (#7048)
Browse files Browse the repository at this point in the history
# Description

Migrating salary pallet to use umbrella crate. It is a follow-up from
#7025
Why did I create this new branch? 
I did this, so that the unnecessary cargo fmt changes from the previous
branch are discarded and hence opened this new PR.



## Review Notes

This PR migrates pallet-salary to use the umbrella crate.

Added change: Explanation requested for why `TestExternalities` was
replaced by `TestState` as testing_prelude already includes it
`pub use sp_io::TestExternalities as TestState;`


I have also modified the defensive! macro to be compatible with umbrella
crate as it was being used in the salary pallet
  • Loading branch information
seemantaggarwal authored Jan 9, 2025
1 parent cdf107d commit 2f17958
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 99 deletions.
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.

17 changes: 17 additions & 0 deletions prdoc/pr_7048.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json

title: '[pallet-salary] Migrate to using frame umbrella crate'

doc:
- audience: Runtime Dev
description: >
This PR migrates the `pallet-salary` to use the FRAME umbrella crate.
This is part of the ongoing effort to migrate all pallets to use the FRAME umbrella crate.
The effort is tracked [here](https://github.com/paritytech/polkadot-sdk/issues/6504).

crates:
- name: pallet-salary
bump: minor
- name: polkadot-sdk-frame
bump: minor
26 changes: 4 additions & 22 deletions substrate/frame/salary/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,25 @@ 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 = ["experimental", "runtime"] }
log = { workspace = true }
pallet-ranked-collective = { optional = true, 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/experimental",
"frame-support/std",
"frame-system/std",
"frame/std",
"log/std",
"pallet-ranked-collective/std",
"scale-info/std",
"sp-arithmetic/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"frame/runtime-benchmarks",
"pallet-ranked-collective/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"frame/try-runtime",
"pallet-ranked-collective?/try-runtime",
"sp-runtime/try-runtime",
]
7 changes: 2 additions & 5 deletions substrate/frame/salary/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
use super::*;
use crate::Pallet as Salary;

use frame_benchmarking::v2::*;
use frame_system::{Pallet as System, RawOrigin};
use sp_core::Get;

use frame::benchmarking::prelude::*;
const SEED: u32 = 0;

fn ensure_member_with_salary<T: Config<I>, I: 'static>(who: &T::AccountId) {
Expand All @@ -37,7 +34,7 @@ fn ensure_member_with_salary<T: Config<I>, I: 'static>(who: &T::AccountId) {
for _ in 0..255 {
let r = T::Members::rank_of(who).expect("prior guard ensures `who` is a member; qed");
if !T::Salary::get_salary(r, &who).is_zero() {
break
break;
}
T::Members::promote(who).unwrap();
}
Expand Down
27 changes: 7 additions & 20 deletions substrate/frame/salary/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,10 @@
#![cfg_attr(not(feature = "std"), no_std)]

use codec::{Decode, Encode, MaxEncodedLen};
use core::marker::PhantomData;
use scale_info::TypeInfo;
use sp_arithmetic::traits::{Saturating, Zero};
use sp_runtime::{Perbill, RuntimeDebug};

use frame_support::{
defensive,
dispatch::DispatchResultWithPostInfo,
ensure,
traits::{
tokens::{GetSalary, Pay, PaymentStatus},
RankedMembers, RankedMembersSwapHandler,
},
use frame::{
prelude::*,
traits::tokens::{GetSalary, Pay, PaymentStatus},
};

#[cfg(test)]
Expand Down Expand Up @@ -85,12 +75,9 @@ pub struct ClaimantStatus<CycleIndex, Balance, Id> {
status: ClaimState<Balance, Id>,
}

#[frame_support::pallet]
#[frame::pallet]
pub mod pallet {
use super::*;
use frame_support::{dispatch::Pays, pallet_prelude::*};
use frame_system::pallet_prelude::*;

#[pallet::pallet]
pub struct Pallet<T, I = ()>(PhantomData<(T, I)>);

Expand Down Expand Up @@ -460,15 +447,15 @@ impl<T: Config<I>, I: 'static>
) {
if who == new_who {
defensive!("Should not try to swap with self");
return
return;
}
if Claimant::<T, I>::contains_key(new_who) {
defensive!("Should not try to overwrite existing claimant");
return
return;
}

let Some(claimant) = Claimant::<T, I>::take(who) else {
frame_support::defensive!("Claimant should exist when swapping");
defensive!("Claimant should exist when swapping");
return;
};

Expand Down
25 changes: 7 additions & 18 deletions substrate/frame/salary/src/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,14 @@
use crate as pallet_salary;
use crate::*;
use frame_support::{
assert_noop, assert_ok, derive_impl, hypothetically,
pallet_prelude::Weight,
parameter_types,
traits::{ConstU64, EitherOf, MapSuccess, NoOpPoll},
};
use frame::{deps::sp_io, testing_prelude::*};
use pallet_ranked_collective::{EnsureRanked, Geometric};
use sp_core::{ConstU16, Get};
use sp_runtime::{
traits::{Convert, ReduceBy, ReplaceWithDefault},
BuildStorage,
};

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

frame_support::construct_runtime!(
pub enum Test
{
construct_runtime!(
pub struct Test {
System: frame_system,
Salary: pallet_salary,
Club: pallet_ranked_collective,
Expand Down Expand Up @@ -145,9 +134,9 @@ impl pallet_ranked_collective::Config for Test {
type BenchmarkSetup = Salary;
}

pub fn new_test_ext() -> sp_io::TestExternalities {
pub fn new_test_ext() -> 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 @@ -194,7 +183,7 @@ fn swap_exhaustive_works() {

// The events mess up the storage root:
System::reset_events();
sp_io::storage::root(sp_runtime::StateVersion::V1)
sp_io::storage::root(StateVersion::V1)
});

let root_swap = hypothetically!({
Expand All @@ -207,7 +196,7 @@ fn swap_exhaustive_works() {

// The events mess up the storage root:
System::reset_events();
sp_io::storage::root(sp_runtime::StateVersion::V1)
sp_io::storage::root(StateVersion::V1)
});

assert_eq!(root_add, root_swap);
Expand Down
24 changes: 8 additions & 16 deletions substrate/frame/salary/src/tests/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,15 @@

//! The crate's tests.
use std::collections::BTreeMap;

use core::cell::RefCell;
use frame_support::{
assert_noop, assert_ok, derive_impl,
pallet_prelude::Weight,
parameter_types,
traits::{tokens::ConvertRank, ConstU64},
};
use sp_runtime::{traits::Identity, BuildStorage, DispatchResult};

use crate as pallet_salary;
use crate::*;
use core::cell::RefCell;
use frame::{deps::sp_runtime::traits::Identity, testing_prelude::*, traits::tokens::ConvertRank};
use std::collections::BTreeMap;

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

frame_support::construct_runtime!(
construct_runtime!(
pub enum Test
{
System: frame_system,
Expand Down Expand Up @@ -124,7 +116,7 @@ impl RankedMembers for TestClub {
}
fn demote(who: &Self::AccountId) -> DispatchResult {
CLUB.with(|club| match club.borrow().get(who) {
None => Err(sp_runtime::DispatchError::Unavailable),
None => Err(DispatchError::Unavailable),
Some(&0) => {
club.borrow_mut().remove(&who);
Ok(())
Expand Down Expand Up @@ -156,9 +148,9 @@ impl Config for Test {
type Budget = Budget;
}

pub fn new_test_ext() -> sp_io::TestExternalities {
pub fn new_test_ext() -> 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
2 changes: 1 addition & 1 deletion substrate/frame/salary/src/weights.rs

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

23 changes: 13 additions & 10 deletions substrate/frame/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,12 @@ 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, EstimateNextSessionRotation, IsSubType, OnRuntimeUpgrade, OneSessionHandler,
pub use frame_support::{
defensive, defensive_assert,
traits::{
Contains, EitherOf, EstimateNextSessionRotation, IsSubType, MapSuccess, NoOpPoll,
OnRuntimeUpgrade, OneSessionHandler, RankedMembers, RankedMembersSwapHandler,
},
};

/// Pallet prelude of `frame-system`.
Expand All @@ -228,11 +232,10 @@ pub mod prelude {
/// Runtime traits
#[doc(no_inline)]
pub use sp_runtime::traits::{
BlockNumberProvider, Bounded, DispatchInfoOf, Dispatchable, SaturatedConversion,
Saturating, StaticLookup, TrailingZeroInput,
BlockNumberProvider, Bounded, Convert, DispatchInfoOf, Dispatchable, ReduceBy,
ReplaceWithDefault, SaturatedConversion, Saturating, StaticLookup, TrailingZeroInput,
};

/// Other runtime types and traits
/// Other error/result types for runtime
#[doc(no_inline)]
pub use sp_runtime::{
BoundToRuntimeAppPublic, DispatchErrorWithPostInfo, DispatchResultWithInfo, TokenError,
Expand Down Expand Up @@ -262,7 +265,7 @@ pub mod benchmarking {
pub use frame_benchmarking::benchmarking::*;
// The system origin, which is very often needed in benchmarking code. Might be tricky only
// if the pallet defines its own `#[pallet::origin]` and call it `RawOrigin`.
pub use frame_system::RawOrigin;
pub use frame_system::{Pallet as System, RawOrigin};
}

#[deprecated(
Expand Down Expand Up @@ -319,7 +322,7 @@ pub mod testing_prelude {
/// Other helper macros from `frame_support` that help with asserting in tests.
pub use frame_support::{
assert_err, assert_err_ignore_postinfo, assert_error_encoded_size, assert_noop, assert_ok,
assert_storage_noop, storage_alias,
assert_storage_noop, hypothetically, storage_alias,
};

pub use frame_system::{self, mocking::*};
Expand All @@ -330,7 +333,7 @@ pub mod testing_prelude {
pub use sp_io::TestExternalities as TestState;

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

/// All of the types and tools needed to build FRAME-based runtimes.
Expand Down Expand Up @@ -508,7 +511,7 @@ pub mod runtime {
#[cfg(feature = "std")]
pub mod testing_prelude {
pub use sp_core::storage::Storage;
pub use sp_runtime::BuildStorage;
pub use sp_runtime::{BuildStorage, DispatchError};
}
}

Expand Down

0 comments on commit 2f17958

Please sign in to comment.