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

Migrating salary pallet to use umbrella crate #7048

Merged
merged 38 commits into from
Jan 9, 2025

Conversation

seemantaggarwal
Copy link
Contributor

@seemantaggarwal seemantaggarwal commented Jan 6, 2025

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

@seemantaggarwal seemantaggarwal added R0-silent Changes should not be mentioned in any release notes T2-pallets This PR/Issue is related to a particular pallet. labels Jan 6, 2025
@seemantaggarwal seemantaggarwal requested a review from a team as a code owner January 6, 2025 07:17
substrate/frame/salary/src/benchmarking.rs Outdated Show resolved Hide resolved
substrate/frame/salary/src/benchmarking.rs Show resolved Hide resolved
substrate/frame/salary/src/lib.rs Outdated Show resolved Hide resolved
substrate/frame/salary/src/lib.rs Outdated Show resolved Hide resolved
substrate/frame/salary/src/tests/integration.rs Outdated Show resolved Hide resolved
substrate/frame/support/src/traits.rs Outdated Show resolved Hide resolved
@paritytech-review-bot paritytech-review-bot bot requested a review from a team January 6, 2025 15:01
Copy link
Contributor

@kianenigma kianenigma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of things are imported manually that are likely in the prelude or else must be added, otherwise looks good!

substrate/frame/salary/src/lib.rs Outdated Show resolved Hide resolved
substrate/frame/salary/src/lib.rs Outdated Show resolved Hide resolved
substrate/frame/salary/src/tests/integration.rs Outdated Show resolved Hide resolved
substrate/frame/salary/src/tests/unit.rs Outdated Show resolved Hide resolved
@@ -46,8 +46,10 @@
#![allow(unused_imports)]
#![allow(missing_docs)]

use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};
use crate::*;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add some explanation as to why you think this is needed before resolving the thread again? You can add a commit link if you want or any explanation.

@@ -46,8 +46,10 @@
#![allow(unused_imports)]
#![allow(missing_docs)]

use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};
use crate::*;
use frame::prelude::*;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This again is not needed. All the imports are already in scope.

- use frame::prelude::*;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add some reasoning as to why you think this is needed before resolving the thread? I believe you shouldn't need this.

substrate/frame/src/lib.rs Outdated Show resolved Hide resolved
substrate/frame/src/lib.rs Outdated Show resolved Hide resolved
@paritytech-review-bot paritytech-review-bot bot requested a review from a team January 6, 2025 16:51
Copy link
Contributor

@gui1117 gui1117 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, some comments about prelude.

defensive,
dispatch::DispatchResultWithPostInfo,
ensure,

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

personal nits, separating imports in categories is a not needed overhead IMHO.

Suggested change

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automatic change by fmt

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The trick is that if you don't have any extra newlines between use statements, they will indeed be grouped together and rustfmt will sort them for you. I think @gui1117 's comment is towards removing newlines in between use statements.

pallet_prelude::Weight,
parameter_types,
traits::{ConstU64, EitherOf, MapSuccess, NoOpPoll},

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cargo fmt will put crate above other import by default

Suggested change

@@ -20,20 +20,14 @@
use std::collections::BTreeMap;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

defensive, defensive_assert,
traits::{
tokens::ConvertRank, Contains, EitherOf, EstimateNextSessionRotation, IsSubType,
MapSuccess, NoOpPoll, OnRuntimeUpgrade, OneSessionHandler,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure ConvertRank is really prelude but it can be ok.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our rule here from other reviews was that if a trait is used in 3 or more pallets, it can be in prelude, if not directly imported in the migration.

@@ -505,7 +508,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::Unavailable};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure Unavailable fits into prelude, DispatchError should be enough I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the comments @gui1117 I added a new commit for PRdoc, can you re review? additionally, for the current pallet only Unavailable was needed, I can have a separate PR for modifying this

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I do think it is a bit too much to import enum variants in any prelude -- the Enum itself makes a lot of sense. DispatchError is super popular, and 100% should be in prelude.

If a code wants to use Unavailable, they would just use DispatchError::Unavailable and that is fine.

Please add this (among other guidelines) to substrate/frame/src/lib.rs, in the top of the file, there is some high level doc, and a section exactly explaining the rules of the umbrella crate.

@github-actions github-actions bot requested a review from gui1117 January 7, 2025 09:47
Copy link

github-actions bot commented Jan 7, 2025

Review required! Latest push from author must always be reviewed

@@ -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, construct_runtime, storage_alias,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure construct_runtime is not already in runtime::prelude::*? I would 99% be sure it already is.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, it is also there in testing_prelude.

@@ -66,15 +66,15 @@ impl<T: VariantCount> Get<u32> for VariantCountOf<T> {
#[macro_export]
macro_rules! defensive {
() => {
frame_support::__private::log::error!(
$crate::__private::log::error!(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very good!

Copy link
Contributor

@kianenigma kianenigma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM after current comments are addressed.

substrate/frame/salary/src/benchmarking.rs Show resolved Hide resolved
parameter_types,
traits::{ConstU64, EitherOf, MapSuccess, NoOpPoll},
use crate::{
tests::integration::sp_api_hidden_includes_construct_runtime::hidden_include::sp_runtime, *,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not import sp_runtime from the umbrella pallet directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let me crosscheck this, this was not a conscious change for sure

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes hidden_includes are generated by the macro for the macro usage, it can break any time.

substrate/frame/salary/src/tests/unit.rs Show resolved Hide resolved
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing the line space between two import blocks causes them to be sorted alphabetically by the fmt. I don't have a strong preference but to be consistent, we should keep imports from the standard library in a separate block (having a line space at the end) at the top.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I would prefer to not put in a separate block. If it is meant to be in a particular order then fmt should do it, same as they put crate and super out of the alphabetic order.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

other comments addressed, leaving this as is

@@ -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
Copy link
Contributor

@UtkarshBhardwaj007 UtkarshBhardwaj007 Jan 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the comments with descriptions when you add new re-exports to the umbrella crate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Raworigin is still intacte

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant could you also add a description for frame_system::Pallet like there exists for RawOrigin.

Copy link
Contributor

@kianenigma kianenigma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushing one last comment with a few fixes:

  • Identity works fine if imported from sp_runtime
  • RankedMembers, RankedMembersSwapHandler are both used in 3 pallets, should go to prelude (as per the conventions of the migration)
  • StateVersion is used in the testing setup of the migration, same.

diff:

diff --git a/substrate/frame/salary/src/lib.rs b/substrate/frame/salary/src/lib.rs
index 256d1ad0cd..6a843625f4 100644
--- a/substrate/frame/salary/src/lib.rs
+++ b/substrate/frame/salary/src/lib.rs
@@ -22,10 +22,7 @@
 use core::marker::PhantomData;
 use frame::{
 	prelude::*,
-	traits::{
-		tokens::{GetSalary, Pay, PaymentStatus},
-		RankedMembers, RankedMembersSwapHandler,
-	},
+	traits::tokens::{GetSalary, Pay, PaymentStatus},
 };
 
 #[cfg(test)]
diff --git a/substrate/frame/salary/src/tests/integration.rs b/substrate/frame/salary/src/tests/integration.rs
index 6d38a1c9b0..e4e9c8f6a3 100644
--- a/substrate/frame/salary/src/tests/integration.rs
+++ b/substrate/frame/salary/src/tests/integration.rs
@@ -19,10 +19,7 @@
 
 use crate as pallet_salary;
 use crate::*;
-use frame::{
-	deps::{sp_io, sp_runtime::StateVersion},
-	testing_prelude::*,
-};
+use frame::{deps::sp_io, testing_prelude::*};
 use pallet_ranked_collective::{EnsureRanked, Geometric};
 
 type Rank = u16;
diff --git a/substrate/frame/salary/src/tests/unit.rs b/substrate/frame/salary/src/tests/unit.rs
index 3bb7bc4adf..6bdd86ddef 100644
--- a/substrate/frame/salary/src/tests/unit.rs
+++ b/substrate/frame/salary/src/tests/unit.rs
@@ -20,7 +20,7 @@
 use crate as pallet_salary;
 use crate::*;
 use core::cell::RefCell;
-use frame::{deps::sp_runtime::traits::Identity, testing_prelude::*, traits::tokens::ConvertRank};
+use frame::{testing_prelude::*, traits::tokens::ConvertRank};
 use std::collections::BTreeMap;
 
 type Block = MockBlock<Test>;
diff --git a/substrate/frame/src/lib.rs b/substrate/frame/src/lib.rs
index dba039bee2..1795dfeb81 100644
--- a/substrate/frame/src/lib.rs
+++ b/substrate/frame/src/lib.rs
@@ -207,7 +207,7 @@ pub mod prelude {
 		defensive, defensive_assert,
 		traits::{
 			Contains, EitherOf, EstimateNextSessionRotation, IsSubType, MapSuccess, NoOpPoll,
-			OnRuntimeUpgrade, OneSessionHandler,
+			OnRuntimeUpgrade, OneSessionHandler, RankedMembers, RankedMembersSwapHandler,
 		},
 	};
 
@@ -232,7 +232,7 @@ pub mod prelude {
 	/// Runtime traits
 	#[doc(no_inline)]
 	pub use sp_runtime::traits::{
-		BlockNumberProvider, Bounded, Convert, DispatchInfoOf, Dispatchable, ReduceBy,
+		BlockNumberProvider, Bounded, Convert, DispatchInfoOf, Dispatchable, Identity, ReduceBy,
 		ReplaceWithDefault, SaturatedConversion, Saturating, StaticLookup, TrailingZeroInput,
 	};
 	/// Other error/result types for runtime
@@ -333,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.

LGTM after applying the above patch

@paritytech-workflow-stopper
Copy link

All GitHub workflows were cancelled due to failure one of the required jobs.
Failed workflow url: https://github.com/paritytech/polkadot-sdk/actions/runs/12674245840
Failed job name: test-linux-stable

Copy link
Contributor

@UtkarshBhardwaj007 UtkarshBhardwaj007 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. 2 points:

  1. 25 CI workflows are failing. Could be a red-herring but the number is suspiciously large so we should check to ensure that the failures are unrelated and raise them in the CI help desk.

  2. Add one small comment about adding description for new re-exports that are added to any of the preludes

@iulianbarbu iulianbarbu removed the request for review from AKJUS January 9, 2025 08:17
@iulianbarbu iulianbarbu removed the R0-silent Changes should not be mentioned in any release notes label Jan 9, 2025
auto-merge was automatically disabled January 9, 2025 13:41

Pull request was closed

@seemantaggarwal seemantaggarwal removed the request for review from AKJUS January 9, 2025 13:45
@seemantaggarwal seemantaggarwal added this pull request to the merge queue Jan 9, 2025
Merged via the queue into master with commit 2f17958 Jan 9, 2025
320 of 347 checks passed
@seemantaggarwal seemantaggarwal deleted the new-salary-pallet branch January 9, 2025 15:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T2-pallets This PR/Issue is related to a particular pallet.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants