From 2954f6a524ed96853b8569738b11d8e23d05432d Mon Sep 17 00:00:00 2001 From: bennethxyz Date: Mon, 18 Nov 2024 14:11:17 -0800 Subject: [PATCH 01/14] frame-tips --- Cargo.lock | 8 +--- substrate/frame/src/lib.rs | 1 + substrate/frame/tips/Cargo.toml | 25 ++---------- substrate/frame/tips/src/lib.rs | 17 ++++++--- .../tips/src/migrations/unreserve_deposits.rs | 19 ++++++---- substrate/frame/tips/src/migrations/v4.rs | 19 ++++++---- substrate/frame/tips/src/tests.rs | 38 ++++++++++--------- substrate/frame/tips/src/weights.rs | 3 +- 8 files changed, 61 insertions(+), 69 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 182d8f6bacad..21cd3b73ce25 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -15487,19 +15487,13 @@ dependencies = [ name = "pallet-tips" version = "27.0.0" dependencies = [ - "frame-benchmarking 28.0.0", - "frame-support 28.0.0", - "frame-system 28.0.0", "log", "pallet-balances 28.0.0", "pallet-treasury 27.0.0", "parity-scale-codec", + "polkadot-sdk-frame 0.1.0", "scale-info", "serde", - "sp-core 28.0.0", - "sp-io 30.0.0", - "sp-runtime 31.0.1", - "sp-storage 19.0.0", ] [[package]] diff --git a/substrate/frame/src/lib.rs b/substrate/frame/src/lib.rs index 0ca36ca8545a..0474468c8e36 100644 --- a/substrate/frame/src/lib.rs +++ b/substrate/frame/src/lib.rs @@ -494,6 +494,7 @@ pub mod runtime { pub mod testing_prelude { pub use sp_core::storage::Storage; pub use sp_runtime::BuildStorage; + pub use sp_runtime::DispatchError; } } diff --git a/substrate/frame/tips/Cargo.toml b/substrate/frame/tips/Cargo.toml index 7c7a2d6aa909..e3096e17889b 100644 --- a/substrate/frame/tips/Cargo.toml +++ b/substrate/frame/tips/Cargo.toml @@ -20,47 +20,30 @@ codec = { features = ["derive"], workspace = true } log = { workspace = true } scale-info = { features = ["derive"], workspace = true } serde = { features = ["derive"], optional = true, workspace = true, default-features = true } -frame-benchmarking = { optional = true, workspace = true } -frame-support = { workspace = true } -frame-system = { workspace = true } +frame = { workspace = true, features = ["experimental", "runtime"] } pallet-treasury = { workspace = true } -sp-core = { workspace = true } -sp-io = { workspace = true } -sp-runtime = { workspace = true } [dev-dependencies] pallet-balances = { workspace = true, default-features = true } -sp-storage = { workspace = true, default-features = true } [features] default = ["std"] std = [ "codec/std", - "frame-benchmarking?/std", - "frame-support/std", - "frame-system/std", + "frame/std", "log/std", "pallet-balances/std", "pallet-treasury/std", "scale-info/std", "serde", - "sp-core/std", - "sp-io/std", - "sp-runtime/std", - "sp-storage/std", ] runtime-benchmarks = [ - "frame-benchmarking/runtime-benchmarks", - "frame-support/runtime-benchmarks", - "frame-system/runtime-benchmarks", + "frame/runtime-benchmarks", "pallet-balances/runtime-benchmarks", "pallet-treasury/runtime-benchmarks", - "sp-runtime/runtime-benchmarks", ] try-runtime = [ - "frame-support/try-runtime", - "frame-system/try-runtime", + "frame/try-runtime", "pallet-balances/try-runtime", "pallet-treasury/try-runtime", - "sp-runtime/try-runtime", ] diff --git a/substrate/frame/tips/src/lib.rs b/substrate/frame/tips/src/lib.rs index 67bcdfa0685e..123753c9b4c5 100644 --- a/substrate/frame/tips/src/lib.rs +++ b/substrate/frame/tips/src/lib.rs @@ -61,14 +61,16 @@ pub mod migrations; pub mod weights; extern crate alloc; - +use frame::{prelude::*, traits::{SortedMembers, ContainsLengthBound, OnUnbalanced, BadOrigin, ExistenceRequirement::KeepAlive, ReservableCurrency, AccountIdConversion, Currency}, arithmetic::Percent, runtime::prelude::storage::migration::storage_key_iter}; +/* use sp_runtime::{ traits::{AccountIdConversion, BadOrigin, Hash, StaticLookup, TrailingZeroInput, Zero}, Percent, RuntimeDebug, }; - +*/ use alloc::{vec, vec::Vec}; use codec::{Decode, Encode}; +/* use frame_support::{ ensure, traits::{ @@ -78,9 +80,10 @@ use frame_support::{ Parameter, }; use frame_system::pallet_prelude::BlockNumberFor; +*/ #[cfg(any(feature = "try-runtime", test))] -use sp_runtime::TryRuntimeError; +use frame::try_runtime::TryRuntimeError; pub use pallet::*; pub use weights::WeightInfo; @@ -118,12 +121,14 @@ pub struct OpenTip< finders_fee: bool, } -#[frame_support::pallet] +// #[frame_support::pallet] +#[frame::pallet] pub mod pallet { use super::*; + /* use frame_support::pallet_prelude::*; use frame_system::pallet_prelude::*; - + */ /// The in-code storage version. const STORAGE_VERSION: StorageVersion = StorageVersion::new(4); @@ -617,7 +622,7 @@ impl, I: 'static> Pallet { tips: Vec<(AccountId, Balance)>, } - use frame_support::{migration::storage_key_iter, Twox64Concat}; + // use frame_support::{migration::storage_key_iter, Twox64Concat}; let zero_account = T::AccountId::decode(&mut TrailingZeroInput::new(&[][..])) .expect("infinite input; qed"); diff --git a/substrate/frame/tips/src/migrations/unreserve_deposits.rs b/substrate/frame/tips/src/migrations/unreserve_deposits.rs index afc424309bf4..5996aa6109f3 100644 --- a/substrate/frame/tips/src/migrations/unreserve_deposits.rs +++ b/substrate/frame/tips/src/migrations/unreserve_deposits.rs @@ -20,6 +20,7 @@ use alloc::collections::btree_map::BTreeMap; use core::iter::Sum; +/* use frame_support::{ pallet_prelude::OptionQuery, storage_alias, @@ -28,6 +29,8 @@ use frame_support::{ Parameter, Twox64Concat, }; use sp_runtime::{traits::Zero, Saturating}; +*/ +use frame::{prelude::*, storage_alias, traits::{Currency, LockableCurrency, ReservableCurrency}, runtime::prelude::weights::{Weight, RuntimeDbWeight}}; #[cfg(feature = "try-runtime")] const LOG_TARGET: &str = "runtime::tips::migrations::unreserve_deposits"; @@ -48,16 +51,16 @@ pub trait UnlockConfig: 'static { /// Base deposit to report a tip. /// /// Should match the currency type previously used for the pallet, if applicable. - type TipReportDepositBase: sp_core::Get>; + type TipReportDepositBase: Get>; /// Deposit per byte to report a tip. /// /// Should match the currency type previously used for the pallet, if applicable. - type DataDepositPerByte: sp_core::Get>; + type DataDepositPerByte: Get>; /// The name of the pallet as previously configured in /// [`construct_runtime!`](frame_support::construct_runtime). - type PalletName: sp_core::Get<&'static str>; + type PalletName: Get<&'static str>; /// The DB weight as configured in the runtime to calculate the correct weight. - type DbWeight: sp_core::Get; + type DbWeight: Get; /// The block number as configured in the runtime. type BlockNumber: Parameter + Zero + Copy + Ord; } @@ -96,8 +99,8 @@ impl, I: 'static> UnreserveDeposits { /// * `BTreeMap`: Map of account IDs to their respective total /// reserved balance by this pallet /// * `frame_support::weights::Weight`: The weight of this operation. - fn get_deposits() -> (BTreeMap>, frame_support::weights::Weight) { - use sp_core::Get; + fn get_deposits() -> (BTreeMap>, Weight) { + // use sp_core::Get; let mut tips_len = 0; let account_deposits: BTreeMap> = Tips::::iter() @@ -167,8 +170,8 @@ where } /// Executes the migration, unreserving funds that are locked in Tip deposits. - fn on_runtime_upgrade() -> frame_support::weights::Weight { - use frame_support::traits::Get; + fn on_runtime_upgrade() -> Weight { + // use frame_support::traits::Get; // Get staked and deposited balances as reported by this pallet. let (account_deposits, initial_reads) = Self::get_deposits(); diff --git a/substrate/frame/tips/src/migrations/v4.rs b/substrate/frame/tips/src/migrations/v4.rs index 8b0e65c58dbd..4c88e8e9cf6b 100644 --- a/substrate/frame/tips/src/migrations/v4.rs +++ b/substrate/frame/tips/src/migrations/v4.rs @@ -16,9 +16,11 @@ // limitations under the License. use core::str; -use sp_io::hashing::twox_128; + +// use sp_io::hashing::twox_128; use super::super::LOG_TARGET; +/* use frame_support::{ storage::StoragePrefixedMap, traits::{ @@ -27,7 +29,8 @@ use frame_support::{ }, weights::Weight, }; - +*/ +use frame::{prelude::*, traits::STORAGE_VERSION_STORAGE_KEY_POSTFIX, runtime::prelude::{weights::Weight, storage::{migration, KeyPrefixIterator, StoragePrefixedMap}}}; use crate as pallet_tips; /// Migrate the entire storage of this pallet to a new prefix. @@ -62,7 +65,7 @@ pub fn migrate::storage_prefix(); - frame_support::storage::migration::move_storage_from_pallet( + migration::move_storage_from_pallet( storage_prefix, old_pallet_name.as_bytes(), new_pallet_name.as_bytes(), @@ -70,7 +73,7 @@ pub fn migrate::storage_prefix(); - frame_support::storage::migration::move_storage_from_pallet( + migration::move_storage_from_pallet( storage_prefix, old_pallet_name.as_bytes(), new_pallet_name.as_bytes(), @@ -116,7 +119,7 @@ pub fn pre_migrate< let new_pallet_prefix = twox_128(new_pallet_name.as_bytes()); let storage_version_key = twox_128(STORAGE_VERSION_STORAGE_KEY_POSTFIX); - let mut new_pallet_prefix_iter = frame_support::storage::KeyPrefixIterator::new( + let mut new_pallet_prefix_iter = KeyPrefixIterator::new( new_pallet_prefix.to_vec(), new_pallet_prefix.to_vec(), |key| Ok(key.to_vec()), @@ -155,7 +158,7 @@ pub fn post_migrate< // Assert that no `Tips` and `Reasons` storages remains at the old prefix. let old_pallet_prefix = twox_128(old_pallet_name.as_bytes()); let old_tips_key = [&old_pallet_prefix, &twox_128(storage_prefix_tips)[..]].concat(); - let old_tips_key_iter = frame_support::storage::KeyPrefixIterator::new( + let old_tips_key_iter = KeyPrefixIterator::new( old_tips_key.to_vec(), old_tips_key.to_vec(), |_| Ok(()), @@ -163,7 +166,7 @@ pub fn post_migrate< assert_eq!(old_tips_key_iter.count(), 0); let old_reasons_key = [&old_pallet_prefix, &twox_128(storage_prefix_reasons)[..]].concat(); - let old_reasons_key_iter = frame_support::storage::KeyPrefixIterator::new( + let old_reasons_key_iter = KeyPrefixIterator::new( old_reasons_key.to_vec(), old_reasons_key.to_vec(), |_| Ok(()), @@ -174,7 +177,7 @@ pub fn post_migrate< // prefix. // NOTE: storage_version_key is already in the new prefix. let new_pallet_prefix = twox_128(new_pallet_name.as_bytes()); - let new_pallet_prefix_iter = frame_support::storage::KeyPrefixIterator::new( + let new_pallet_prefix_iter = KeyPrefixIterator::new( new_pallet_prefix.to_vec(), new_pallet_prefix.to_vec(), |_| Ok(()), diff --git a/substrate/frame/tips/src/tests.rs b/substrate/frame/tips/src/tests.rs index 530efb708e41..dec88f9f8a45 100644 --- a/substrate/frame/tips/src/tests.rs +++ b/substrate/frame/tips/src/tests.rs @@ -18,8 +18,9 @@ //! Treasury pallet tests. #![cfg(test)] - +/* use sp_core::H256; + use sp_runtime::{ traits::{BadOrigin, BlakeTwo256, IdentityLookup}, BuildStorage, Perbill, Permill, @@ -35,13 +36,14 @@ use frame_support::{ }, PalletId, }; - +*/ +use frame::{testing_prelude::*, runtime::{testing_prelude::DispatchError::Other, prelude::{storage::{migration, StoragePrefixedMap}}}, traits::{PalletInfoAccess, IdentityLookup, NeverEnsureOrigin}, arithmetic::{Perbill, Permill}, deps::frame_support::{PalletId, traits::tokens::{PayFromAccount, UnityAssetBalanceConversion}}}; use super::*; use crate::{self as pallet_tips, Event as TipEvent}; type Block = frame_system::mocking::MockBlock; -frame_support::construct_runtime!( +construct_runtime!( pub enum Test { System: frame_system, @@ -112,7 +114,7 @@ impl pallet_treasury::Config for Test { type WeightInfo = (); type SpendFunds = (); type MaxApprovals = ConstU32<100>; - type SpendOrigin = frame_support::traits::NeverEnsureOrigin; + type SpendOrigin = NeverEnsureOrigin; type AssetKind = (); type Beneficiary = Self::AccountId; type BeneficiaryLookup = IdentityLookup; @@ -135,7 +137,7 @@ impl pallet_treasury::Config for Test { type WeightInfo = (); type SpendFunds = (); type MaxApprovals = ConstU32<100>; - type SpendOrigin = frame_support::traits::NeverEnsureOrigin; + type SpendOrigin = NeverEnsureOrigin; type AssetKind = (); type Beneficiary = Self::AccountId; type BeneficiaryLookup = IdentityLookup; @@ -177,8 +179,8 @@ impl Config for Test { type WeightInfo = (); } -pub fn new_test_ext() -> sp_io::TestExternalities { - let mut ext: sp_io::TestExternalities = RuntimeGenesisConfig { +pub fn new_test_ext() -> TestExternalities { + let mut ext: TestExternalities = RuntimeGenesisConfig { system: frame_system::GenesisConfig::default(), balances: pallet_balances::GenesisConfig { balances: vec![(0, 100), (1, 98), (2, 1)] }, treasury: Default::default(), @@ -485,7 +487,7 @@ fn test_last_reward_migration() { s.top = data.into_iter().collect(); - sp_io::TestExternalities::new(s).execute_with(|| { + TestExternalities::new(s).execute_with(|| { let module = pallet_tips::Tips::::pallet_prefix(); let item = pallet_tips::Tips::::storage_prefix(); Tips::migrate_retract_tip_for_tip_new(module, item); @@ -543,12 +545,12 @@ fn test_migration_v4() { let mut s = Storage::default(); s.top = data.into_iter().collect(); - sp_io::TestExternalities::new(s).execute_with(|| { - use frame_support::traits::PalletInfoAccess; + TestExternalities::new(s).execute_with(|| { + // use frame_support::traits::PalletInfoAccess; let old_pallet = "Treasury"; let new_pallet = ::name(); - frame_support::storage::migration::move_pallet( + migration::move_pallet( new_pallet.as_bytes(), old_pallet.as_bytes(), ); @@ -559,12 +561,12 @@ fn test_migration_v4() { crate::migrations::v4::post_migrate::(old_pallet); }); - sp_io::TestExternalities::new(Storage::default()).execute_with(|| { - use frame_support::traits::PalletInfoAccess; + TestExternalities::new(Storage::default()).execute_with(|| { + // use frame_support::traits::PalletInfoAccess; let old_pallet = "Treasury"; let new_pallet = ::name(); - frame_support::storage::migration::move_pallet( + migration::move_pallet( new_pallet.as_bytes(), old_pallet.as_bytes(), ); @@ -589,7 +591,7 @@ fn genesis_funding_works() { pallet_treasury::GenesisConfig::::default() .assimilate_storage(&mut t) .unwrap(); - let mut t: sp_io::TestExternalities = t.into(); + let mut t: TestExternalities = t.into(); t.execute_with(|| { assert_eq!(Balances::free_balance(Treasury::account_id()), initial_funding); @@ -633,7 +635,7 @@ fn report_awesome_and_tip_works_second_instance() { #[test] fn equal_entries_invariant() { new_test_ext().execute_with(|| { - use frame_support::pallet_prelude::DispatchError::Other; + // use frame_support::pallet_prelude::DispatchError::Other; Balances::make_free_balance_be(&Treasury::account_id(), 101); @@ -666,7 +668,7 @@ fn equal_entries_invariant() { #[test] fn finders_fee_invariant() { new_test_ext().execute_with(|| { - use frame_support::pallet_prelude::DispatchError::Other; + // use frame_support::pallet_prelude::DispatchError::Other; // Breaks invariant by having a zero deposit. TipReportDepositBase::set(0); @@ -686,7 +688,7 @@ fn finders_fee_invariant() { #[test] fn reasons_invariant() { new_test_ext().execute_with(|| { - use frame_support::pallet_prelude::DispatchError::Other; + // use frame_support::pallet_prelude::DispatchError::Other; Balances::make_free_balance_be(&Treasury::account_id(), 101); diff --git a/substrate/frame/tips/src/weights.rs b/substrate/frame/tips/src/weights.rs index e9805e9cc9bf..1d23295e9c3f 100644 --- a/substrate/frame/tips/src/weights.rs +++ b/substrate/frame/tips/src/weights.rs @@ -46,7 +46,8 @@ #![allow(unused_imports)] #![allow(missing_docs)] -use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}}; +// use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}}; +use frame::weights_prelude::*; use core::marker::PhantomData; /// Weight functions needed for `pallet_tips`. From fc2bb13ce4cdd17ca5e37cdd32fa0d40c38ffb67 Mon Sep 17 00:00:00 2001 From: bennethxyz Date: Thu, 5 Dec 2024 10:29:44 -0800 Subject: [PATCH 02/14] less imports adopts prelude prevelance --- prdoc/pr_6532.prdoc | 17 +++++++ substrate/frame/src/lib.rs | 5 +- substrate/frame/tips/src/benchmarking.rs | 18 +++---- substrate/frame/tips/src/lib.rs | 47 ++++++------------- .../tips/src/migrations/unreserve_deposits.rs | 39 ++++++--------- substrate/frame/tips/src/migrations/v4.rs | 41 +++++----------- substrate/frame/tips/src/tests.rs | 42 +++++++---------- 7 files changed, 85 insertions(+), 124 deletions(-) create mode 100644 prdoc/pr_6532.prdoc diff --git a/prdoc/pr_6532.prdoc b/prdoc/pr_6532.prdoc new file mode 100644 index 000000000000..8b35c00bbdba --- /dev/null +++ b/prdoc/pr_6532.prdoc @@ -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: Use frame umbrella crate in pallet-babe and pallet-staking-reward-curve + +doc: + - audience: Runtime Dev + description: | + Adapts the FRAME umbrella crate into pallet-tips, + adds the ensure macro and PalletId type commonly used in mock + runtimes accross different pallets to the prelude. + +crates: + - name: polkadot-sdk-frame + bump: minor + - name: pallet-tips + bump: patch \ No newline at end of file diff --git a/substrate/frame/src/lib.rs b/substrate/frame/src/lib.rs index 0474468c8e36..208b6d04c69f 100644 --- a/substrate/frame/src/lib.rs +++ b/substrate/frame/src/lib.rs @@ -308,7 +308,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, storage_alias, ensure, }; pub use frame_system::{self, mocking::*}; @@ -350,6 +350,9 @@ pub mod runtime { /// Macro to easily derive the `Config` trait of various pallet for `Runtime`. pub use frame_support::derive_impl; + /// sovereign account ID for a pallet. + pub use frame_support::PalletId; + /// Macros to easily impl traits such as `Get` for types. // TODO: using linking in the Get in the line above triggers an ICE :/ pub use frame_support::{ord_parameter_types, parameter_types}; diff --git a/substrate/frame/tips/src/benchmarking.rs b/substrate/frame/tips/src/benchmarking.rs index 4a991b11b933..ca8b2bbabf06 100644 --- a/substrate/frame/tips/src/benchmarking.rs +++ b/substrate/frame/tips/src/benchmarking.rs @@ -19,15 +19,9 @@ #![cfg(feature = "runtime-benchmarks")] -use frame_benchmarking::v1::{ - account, benchmarks_instance_pallet, whitelisted_caller, BenchmarkError, -}; -use frame_support::ensure; -use frame_system::RawOrigin; -use sp_runtime::traits::Saturating; - use super::*; use crate::Pallet as TipsMod; +use frame::benchmarking::prelude::*; const SEED: u32 = 0; @@ -97,7 +91,7 @@ benchmarks_instance_pallet! { let awesome_person_lookup = T::Lookup::unlookup(awesome_person); // Whitelist caller account from further DB operations. let caller_key = frame_system::Account::::hashed_key_for(&caller); - frame_benchmarking::benchmarking::add_to_whitelist(caller_key.into()); + add_to_whitelist(caller_key.into()); }: _(RawOrigin::Signed(caller), reason, awesome_person_lookup) retract_tip { @@ -113,7 +107,7 @@ benchmarks_instance_pallet! { let hash = T::Hashing::hash_of(&(&reason_hash, &awesome_person)); // Whitelist caller account from further DB operations. let caller_key = frame_system::Account::::hashed_key_for(&caller); - frame_benchmarking::benchmarking::add_to_whitelist(caller_key.into()); + add_to_whitelist(caller_key.into()); }: _(RawOrigin::Signed(caller), hash) tip_new { @@ -124,7 +118,7 @@ benchmarks_instance_pallet! { let beneficiary_lookup = T::Lookup::unlookup(beneficiary); // Whitelist caller account from further DB operations. let caller_key = frame_system::Account::::hashed_key_for(&caller); - frame_benchmarking::benchmarking::add_to_whitelist(caller_key.into()); + add_to_whitelist(caller_key.into()); }: _(RawOrigin::Signed(caller), reason, beneficiary_lookup, value) tip { @@ -145,7 +139,7 @@ benchmarks_instance_pallet! { let caller = account("member", t - 1, SEED); // Whitelist caller account from further DB operations. let caller_key = frame_system::Account::::hashed_key_for(&caller); - frame_benchmarking::benchmarking::add_to_whitelist(caller_key.into()); + add_to_whitelist(caller_key.into()); }: _(RawOrigin::Signed(caller), hash, value) close_tip { @@ -175,7 +169,7 @@ benchmarks_instance_pallet! { let caller = account("caller", t, SEED); // Whitelist caller account from further DB operations. let caller_key = frame_system::Account::::hashed_key_for(&caller); - frame_benchmarking::benchmarking::add_to_whitelist(caller_key.into()); + add_to_whitelist(caller_key.into()); }: _(RawOrigin::Signed(caller), hash) slash_tip { diff --git a/substrate/frame/tips/src/lib.rs b/substrate/frame/tips/src/lib.rs index 123753c9b4c5..9ae3a7a4fa77 100644 --- a/substrate/frame/tips/src/lib.rs +++ b/substrate/frame/tips/src/lib.rs @@ -61,29 +61,16 @@ pub mod migrations; pub mod weights; extern crate alloc; -use frame::{prelude::*, traits::{SortedMembers, ContainsLengthBound, OnUnbalanced, BadOrigin, ExistenceRequirement::KeepAlive, ReservableCurrency, AccountIdConversion, Currency}, arithmetic::Percent, runtime::prelude::storage::migration::storage_key_iter}; -/* -use sp_runtime::{ - traits::{AccountIdConversion, BadOrigin, Hash, StaticLookup, TrailingZeroInput, Zero}, - Percent, RuntimeDebug, -}; -*/ use alloc::{vec, vec::Vec}; use codec::{Decode, Encode}; -/* -use frame_support::{ - ensure, +use frame::{ + arithmetic::Percent, + runtime::prelude::*, traits::{ - ContainsLengthBound, Currency, EnsureOrigin, ExistenceRequirement::KeepAlive, Get, - OnUnbalanced, ReservableCurrency, SortedMembers, + AccountIdConversion, BadOrigin, ContainsLengthBound, Currency, + ExistenceRequirement::KeepAlive, OnUnbalanced, ReservableCurrency, SortedMembers, }, - Parameter, }; -use frame_system::pallet_prelude::BlockNumberFor; -*/ - -#[cfg(any(feature = "try-runtime", test))] -use frame::try_runtime::TryRuntimeError; pub use pallet::*; pub use weights::WeightInfo; @@ -121,14 +108,10 @@ pub struct OpenTip< finders_fee: bool, } -// #[frame_support::pallet] #[frame::pallet] pub mod pallet { use super::*; - /* - use frame_support::pallet_prelude::*; - use frame_system::pallet_prelude::*; - */ + /// The in-code storage version. const STORAGE_VERSION: StorageVersion = StorageVersion::new(4); @@ -488,7 +471,7 @@ pub mod pallet { } #[cfg(feature = "try-runtime")] - fn try_state(_n: BlockNumberFor) -> Result<(), TryRuntimeError> { + fn try_state(_n: BlockNumberFor) -> Result<(), frame::try_runtime::TryRuntimeError> { Self::do_try_state() } } @@ -622,12 +605,10 @@ impl, I: 'static> Pallet { tips: Vec<(AccountId, Balance)>, } - // use frame_support::{migration::storage_key_iter, Twox64Concat}; - let zero_account = T::AccountId::decode(&mut TrailingZeroInput::new(&[][..])) .expect("infinite input; qed"); - for (hash, old_tip) in storage_key_iter::< + for (hash, old_tip) in storage::migration::storage_key_iter::< T::Hash, OldOpenTip, BlockNumberFor, T::Hash>, Twox64Concat, @@ -659,14 +640,16 @@ impl, I: 'static> Pallet { /// 1. The number of entries in `Tips` should be equal to `Reasons`. /// 2. Reasons exists for each Tip[`OpenTip.reason`]. /// 3. If `OpenTip.finders_fee` is true, then OpenTip.deposit should be greater than zero. - #[cfg(any(feature = "try-runtime", test))] - pub fn do_try_state() -> Result<(), TryRuntimeError> { + #[cfg(feature = "try-runtime")] + pub fn do_try_state() -> Result<(), frame::try_runtime::TryRuntimeError> { let reasons = Reasons::::iter_keys().collect::>(); let tips = Tips::::iter_keys().collect::>(); ensure!( reasons.len() == tips.len(), - TryRuntimeError::Other("Equal length of entries in `Tips` and `Reasons` Storage") + frame::try_runtime::TryRuntimeError::Other( + "Equal length of entries in `Tips` and `Reasons` Storage" + ) ); for tip in Tips::::iter_keys() { @@ -675,7 +658,7 @@ impl, I: 'static> Pallet { if open_tip.finders_fee { ensure!( !open_tip.deposit.is_zero(), - TryRuntimeError::Other( + frame::try_runtime::TryRuntimeError::Other( "Tips with `finders_fee` should have non-zero `deposit`." ) ) @@ -683,7 +666,7 @@ impl, I: 'static> Pallet { ensure!( reasons.contains(&open_tip.reason), - TryRuntimeError::Other("no reason for this tip") + frame::try_runtime::TryRuntimeError::Other("no reason for this tip") ); } Ok(()) diff --git a/substrate/frame/tips/src/migrations/unreserve_deposits.rs b/substrate/frame/tips/src/migrations/unreserve_deposits.rs index 5996aa6109f3..64d6fe16e6b2 100644 --- a/substrate/frame/tips/src/migrations/unreserve_deposits.rs +++ b/substrate/frame/tips/src/migrations/unreserve_deposits.rs @@ -20,17 +20,11 @@ use alloc::collections::btree_map::BTreeMap; use core::iter::Sum; -/* -use frame_support::{ - pallet_prelude::OptionQuery, +use frame::{ + runtime::prelude::*, storage_alias, - traits::{Currency, LockableCurrency, OnRuntimeUpgrade, ReservableCurrency}, - weights::RuntimeDbWeight, - Parameter, Twox64Concat, + traits::{Currency, LockableCurrency, ReservableCurrency}, }; -use sp_runtime::{traits::Zero, Saturating}; -*/ -use frame::{prelude::*, storage_alias, traits::{Currency, LockableCurrency, ReservableCurrency}, runtime::prelude::weights::{Weight, RuntimeDbWeight}}; #[cfg(feature = "try-runtime")] const LOG_TARGET: &str = "runtime::tips::migrations::unreserve_deposits"; @@ -60,7 +54,7 @@ pub trait UnlockConfig: 'static { /// [`construct_runtime!`](frame_support::construct_runtime). type PalletName: Get<&'static str>; /// The DB weight as configured in the runtime to calculate the correct weight. - type DbWeight: Get; + type DbWeight: Get; /// The block number as configured in the runtime. type BlockNumber: Parameter + Zero + Copy + Ord; } @@ -136,9 +130,9 @@ where /// Fails with a `TryRuntimeError` if somehow the amount reserved by this pallet is greater than /// the actual total reserved amount for any accounts. #[cfg(feature = "try-runtime")] - fn pre_upgrade() -> Result, sp_runtime::TryRuntimeError> { + fn pre_upgrade() -> Result, frame::try_runtime::TryRuntimeError> { use codec::Encode; - use frame_support::ensure; + //use frame_support::ensure; // Get the Tips pallet view of balances it has reserved let (account_deposits, _) = Self::get_deposits(); @@ -193,7 +187,7 @@ where #[cfg(feature = "try-runtime")] fn post_upgrade( account_reserved_before_bytes: alloc::vec::Vec, - ) -> Result<(), sp_runtime::TryRuntimeError> { + ) -> Result<(), frame::try_runtime::TryRuntimeError> { use codec::Decode; let account_reserved_before = BTreeMap::>::decode( @@ -233,13 +227,8 @@ where #[cfg(all(feature = "try-runtime", test))] mod test { use super::*; - use crate::{ - migrations::unreserve_deposits::UnreserveDeposits, - tests::{new_test_ext, Balances, RuntimeOrigin, Test, Tips}, - }; - use frame_support::{assert_ok, parameter_types, traits::TypedGet}; - use frame_system::pallet_prelude::BlockNumberFor; - use sp_core::ConstU64; + use crate::tests::{new_test_ext, Balances, RuntimeOrigin, Test, Tips}; + use frame::testing_prelude::*; parameter_types! { const PalletName: &'static str = "Tips"; @@ -250,7 +239,7 @@ mod test { type Currency = Balances; type TipReportDepositBase = ConstU64<1>; type DataDepositPerByte = ConstU64<1>; - type Hash = sp_core::H256; + type Hash = crate::H256; type AccountId = u128; type BlockNumber = BlockNumberFor; type DbWeight = (); @@ -293,15 +282,15 @@ mod test { assert_eq!( ::Currency::reserved_balance(&tipper_0), tipper_0_initial_reserved + - ::TipReportDepositBase::get() + - ::DataDepositPerByte::get() * + <::TipReportDepositBase as frame::prelude::TypedGet>::get() + + <::DataDepositPerByte as frame::prelude::TypedGet>::get() * tip_0_reason.len() as u64 ); assert_eq!( ::Currency::reserved_balance(&tipper_1), tipper_1_initial_reserved + - ::TipReportDepositBase::get() + - ::DataDepositPerByte::get() * + <::TipReportDepositBase as frame::prelude::TypedGet>::get() + + <::DataDepositPerByte as frame::prelude::TypedGet>::get() * tip_1_reason.len() as u64 ); diff --git a/substrate/frame/tips/src/migrations/v4.rs b/substrate/frame/tips/src/migrations/v4.rs index 4c88e8e9cf6b..0a51ca13ed86 100644 --- a/substrate/frame/tips/src/migrations/v4.rs +++ b/substrate/frame/tips/src/migrations/v4.rs @@ -15,23 +15,12 @@ // See the License for the specific language governing permissions and // limitations under the License. +use super::super::LOG_TARGET; use core::str; -// use sp_io::hashing::twox_128; - -use super::super::LOG_TARGET; -/* -use frame_support::{ - storage::StoragePrefixedMap, - traits::{ - Get, GetStorageVersion, PalletInfoAccess, StorageVersion, - STORAGE_VERSION_STORAGE_KEY_POSTFIX, - }, - weights::Weight, -}; -*/ -use frame::{prelude::*, traits::STORAGE_VERSION_STORAGE_KEY_POSTFIX, runtime::prelude::{weights::Weight, storage::{migration, KeyPrefixIterator, StoragePrefixedMap}}}; use crate as pallet_tips; +use frame::{runtime::prelude::*, traits::STORAGE_VERSION_STORAGE_KEY_POSTFIX}; +use storage::StoragePrefixedMap as _; /// Migrate the entire storage of this pallet to a new prefix. /// @@ -65,7 +54,7 @@ pub fn migrate::storage_prefix(); - migration::move_storage_from_pallet( + storage::migration::move_storage_from_pallet( storage_prefix, old_pallet_name.as_bytes(), new_pallet_name.as_bytes(), @@ -73,7 +62,7 @@ pub fn migrate::storage_prefix(); - migration::move_storage_from_pallet( + storage::migration::move_storage_from_pallet( storage_prefix, old_pallet_name.as_bytes(), new_pallet_name.as_bytes(), @@ -119,7 +108,7 @@ pub fn pre_migrate< let new_pallet_prefix = twox_128(new_pallet_name.as_bytes()); let storage_version_key = twox_128(STORAGE_VERSION_STORAGE_KEY_POSTFIX); - let mut new_pallet_prefix_iter = KeyPrefixIterator::new( + let mut new_pallet_prefix_iter = storage::KeyPrefixIterator::new( new_pallet_prefix.to_vec(), new_pallet_prefix.to_vec(), |key| Ok(key.to_vec()), @@ -158,26 +147,22 @@ pub fn post_migrate< // Assert that no `Tips` and `Reasons` storages remains at the old prefix. let old_pallet_prefix = twox_128(old_pallet_name.as_bytes()); let old_tips_key = [&old_pallet_prefix, &twox_128(storage_prefix_tips)[..]].concat(); - let old_tips_key_iter = KeyPrefixIterator::new( - old_tips_key.to_vec(), - old_tips_key.to_vec(), - |_| Ok(()), - ); + let old_tips_key_iter = + storage::KeyPrefixIterator::new(old_tips_key.to_vec(), old_tips_key.to_vec(), |_| Ok(())); assert_eq!(old_tips_key_iter.count(), 0); let old_reasons_key = [&old_pallet_prefix, &twox_128(storage_prefix_reasons)[..]].concat(); - let old_reasons_key_iter = KeyPrefixIterator::new( - old_reasons_key.to_vec(), - old_reasons_key.to_vec(), - |_| Ok(()), - ); + let old_reasons_key_iter = + storage::KeyPrefixIterator::new(old_reasons_key.to_vec(), old_reasons_key.to_vec(), |_| { + Ok(()) + }); assert_eq!(old_reasons_key_iter.count(), 0); // Assert that the `Tips` and `Reasons` storages (if they exist) have been moved to the new // prefix. // NOTE: storage_version_key is already in the new prefix. let new_pallet_prefix = twox_128(new_pallet_name.as_bytes()); - let new_pallet_prefix_iter = KeyPrefixIterator::new( + let new_pallet_prefix_iter = storage::KeyPrefixIterator::new( new_pallet_prefix.to_vec(), new_pallet_prefix.to_vec(), |_| Ok(()), diff --git a/substrate/frame/tips/src/tests.rs b/substrate/frame/tips/src/tests.rs index dec88f9f8a45..e18c81a88007 100644 --- a/substrate/frame/tips/src/tests.rs +++ b/substrate/frame/tips/src/tests.rs @@ -18,28 +18,20 @@ //! Treasury pallet tests. #![cfg(test)] -/* -use sp_core::H256; - -use sp_runtime::{ - traits::{BadOrigin, BlakeTwo256, IdentityLookup}, - BuildStorage, Perbill, Permill, -}; -use sp_storage::Storage; - -use frame_support::{ - assert_noop, assert_ok, derive_impl, parameter_types, - storage::StoragePrefixedMap, +use super::*; +use crate::{self as pallet_tips, Event as TipEvent}; +use frame::{ + arithmetic::{Perbill, Permill}, + testing_prelude::*, traits::{ tokens::{PayFromAccount, UnityAssetBalanceConversion}, - ConstU32, ConstU64, IntegrityTest, SortedMembers, StorageVersion, + IdentityLookup, NeverEnsureOrigin, PalletInfoAccess, }, - PalletId, }; -*/ -use frame::{testing_prelude::*, runtime::{testing_prelude::DispatchError::Other, prelude::{storage::{migration, StoragePrefixedMap}}}, traits::{PalletInfoAccess, IdentityLookup, NeverEnsureOrigin}, arithmetic::{Perbill, Permill}, deps::frame_support::{PalletId, traits::tokens::{PayFromAccount, UnityAssetBalanceConversion}}}; -use super::*; -use crate::{self as pallet_tips, Event as TipEvent}; +use storage::StoragePrefixedMap as _; + +#[cfg(feature = "try-runtime")] +use crate::DispatchError::Other; type Block = frame_system::mocking::MockBlock; @@ -197,6 +189,7 @@ pub fn new_test_ext() -> TestExternalities { pub fn build_and_execute(test: impl FnOnce() -> ()) { new_test_ext().execute_with(|| { test(); + #[cfg(feature = "try-runtime")] Tips::do_try_state().expect("All invariants must hold after a test"); }); } @@ -550,10 +543,7 @@ fn test_migration_v4() { let old_pallet = "Treasury"; let new_pallet = ::name(); - migration::move_pallet( - new_pallet.as_bytes(), - old_pallet.as_bytes(), - ); + storage::migration::move_pallet(new_pallet.as_bytes(), old_pallet.as_bytes()); StorageVersion::new(0).put::(); crate::migrations::v4::pre_migrate::(old_pallet); @@ -566,10 +556,7 @@ fn test_migration_v4() { let old_pallet = "Treasury"; let new_pallet = ::name(); - migration::move_pallet( - new_pallet.as_bytes(), - old_pallet.as_bytes(), - ); + storage::migration::move_pallet(new_pallet.as_bytes(), old_pallet.as_bytes()); StorageVersion::new(0).put::(); crate::migrations::v4::pre_migrate::(old_pallet); @@ -658,6 +645,7 @@ fn equal_entries_invariant() { pallet_tips::Tips::::insert(hash1, tip); // Invariant violated + #[cfg(feature = "try-runtime")] assert_eq!( Tips::do_try_state(), Err(Other("Equal length of entries in `Tips` and `Reasons` Storage")) @@ -678,6 +666,7 @@ fn finders_fee_invariant() { assert_ok!(Tips::report_awesome(RuntimeOrigin::signed(0), b"".to_vec(), 3)); // Invariant violated + #[cfg(feature = "try-runtime")] assert_eq!( Tips::do_try_state(), Err(Other("Tips with `finders_fee` should have non-zero `deposit`.")) @@ -704,6 +693,7 @@ fn reasons_invariant() { pallet_tips::Tips::::insert(hash[0], open_tip); // Invariant violated + #[cfg(feature = "try-runtime")] assert_eq!(Tips::do_try_state(), Err(Other("no reason for this tip"))); }) } From e9faacd06340eb924ddfe43f12c05567337bfeb8 Mon Sep 17 00:00:00 2001 From: bennethxyz Date: Thu, 5 Dec 2024 12:14:02 -0800 Subject: [PATCH 03/14] nit: remove comments --- .../tips/src/migrations/unreserve_deposits.rs | 14 +++++--------- substrate/frame/tips/src/tests.rs | 10 ---------- substrate/frame/tips/src/weights.rs | 1 - 3 files changed, 5 insertions(+), 20 deletions(-) diff --git a/substrate/frame/tips/src/migrations/unreserve_deposits.rs b/substrate/frame/tips/src/migrations/unreserve_deposits.rs index 64d6fe16e6b2..53d0d25335ec 100644 --- a/substrate/frame/tips/src/migrations/unreserve_deposits.rs +++ b/substrate/frame/tips/src/migrations/unreserve_deposits.rs @@ -94,8 +94,6 @@ impl, I: 'static> UnreserveDeposits { /// reserved balance by this pallet /// * `frame_support::weights::Weight`: The weight of this operation. fn get_deposits() -> (BTreeMap>, Weight) { - // use sp_core::Get; - let mut tips_len = 0; let account_deposits: BTreeMap> = Tips::::iter() .map(|(_hash, open_tip)| open_tip) @@ -165,8 +163,6 @@ where /// Executes the migration, unreserving funds that are locked in Tip deposits. fn on_runtime_upgrade() -> Weight { - // use frame_support::traits::Get; - // Get staked and deposited balances as reported by this pallet. let (account_deposits, initial_reads) = Self::get_deposits(); @@ -282,15 +278,15 @@ mod test { assert_eq!( ::Currency::reserved_balance(&tipper_0), tipper_0_initial_reserved + - <::TipReportDepositBase as frame::prelude::TypedGet>::get() + - <::DataDepositPerByte as frame::prelude::TypedGet>::get() * - tip_0_reason.len() as u64 + <::TipReportDepositBase as TypedGet>::get() + + <::DataDepositPerByte as frame::prelude::TypedGet>::get( + ) * tip_0_reason.len() as u64 ); assert_eq!( ::Currency::reserved_balance(&tipper_1), tipper_1_initial_reserved + - <::TipReportDepositBase as frame::prelude::TypedGet>::get() + - <::DataDepositPerByte as frame::prelude::TypedGet>::get() * + <::TipReportDepositBase as TypedGet>::get() + + <::DataDepositPerByte as TypedGet>::get() * tip_1_reason.len() as u64 ); diff --git a/substrate/frame/tips/src/tests.rs b/substrate/frame/tips/src/tests.rs index e18c81a88007..94caa863fa9c 100644 --- a/substrate/frame/tips/src/tests.rs +++ b/substrate/frame/tips/src/tests.rs @@ -539,8 +539,6 @@ fn test_migration_v4() { s.top = data.into_iter().collect(); TestExternalities::new(s).execute_with(|| { - // use frame_support::traits::PalletInfoAccess; - let old_pallet = "Treasury"; let new_pallet = ::name(); storage::migration::move_pallet(new_pallet.as_bytes(), old_pallet.as_bytes()); @@ -552,8 +550,6 @@ fn test_migration_v4() { }); TestExternalities::new(Storage::default()).execute_with(|| { - // use frame_support::traits::PalletInfoAccess; - let old_pallet = "Treasury"; let new_pallet = ::name(); storage::migration::move_pallet(new_pallet.as_bytes(), old_pallet.as_bytes()); @@ -622,8 +618,6 @@ fn report_awesome_and_tip_works_second_instance() { #[test] fn equal_entries_invariant() { new_test_ext().execute_with(|| { - // use frame_support::pallet_prelude::DispatchError::Other; - Balances::make_free_balance_be(&Treasury::account_id(), 101); assert_ok!(Tips::report_awesome(RuntimeOrigin::signed(0), b"awesome.dot".to_vec(), 3)); @@ -656,8 +650,6 @@ fn equal_entries_invariant() { #[test] fn finders_fee_invariant() { new_test_ext().execute_with(|| { - // use frame_support::pallet_prelude::DispatchError::Other; - // Breaks invariant by having a zero deposit. TipReportDepositBase::set(0); @@ -677,8 +669,6 @@ fn finders_fee_invariant() { #[test] fn reasons_invariant() { new_test_ext().execute_with(|| { - // use frame_support::pallet_prelude::DispatchError::Other; - Balances::make_free_balance_be(&Treasury::account_id(), 101); assert_ok!(Tips::report_awesome(RuntimeOrigin::signed(0), b"awesome.dot".to_vec(), 0)); diff --git a/substrate/frame/tips/src/weights.rs b/substrate/frame/tips/src/weights.rs index 1d23295e9c3f..4c8359f0e66f 100644 --- a/substrate/frame/tips/src/weights.rs +++ b/substrate/frame/tips/src/weights.rs @@ -46,7 +46,6 @@ #![allow(unused_imports)] #![allow(missing_docs)] -// use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}}; use frame::weights_prelude::*; use core::marker::PhantomData; From 7a30bf402948e8467be468d904b75523eb427298 Mon Sep 17 00:00:00 2001 From: bennethxyz Date: Thu, 5 Dec 2024 12:17:40 -0800 Subject: [PATCH 04/14] nit: same as before --- substrate/frame/tips/src/migrations/unreserve_deposits.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/substrate/frame/tips/src/migrations/unreserve_deposits.rs b/substrate/frame/tips/src/migrations/unreserve_deposits.rs index 53d0d25335ec..704b9f81166d 100644 --- a/substrate/frame/tips/src/migrations/unreserve_deposits.rs +++ b/substrate/frame/tips/src/migrations/unreserve_deposits.rs @@ -130,7 +130,6 @@ where #[cfg(feature = "try-runtime")] fn pre_upgrade() -> Result, frame::try_runtime::TryRuntimeError> { use codec::Encode; - //use frame_support::ensure; // Get the Tips pallet view of balances it has reserved let (account_deposits, _) = Self::get_deposits(); From 205ee2a8d85c8b31d4601e803faec305ac6809ad Mon Sep 17 00:00:00 2001 From: bennethxyz Date: Wed, 11 Dec 2024 20:26:25 -0800 Subject: [PATCH 05/14] remove prdoc --- prdoc/pr_6532.prdoc | 17 ----------------- substrate/frame/src/lib.rs | 8 +++++--- substrate/frame/tips/src/lib.rs | 1 - substrate/frame/tips/src/tests.rs | 1 - 4 files changed, 5 insertions(+), 22 deletions(-) delete mode 100644 prdoc/pr_6532.prdoc diff --git a/prdoc/pr_6532.prdoc b/prdoc/pr_6532.prdoc deleted file mode 100644 index 8b35c00bbdba..000000000000 --- a/prdoc/pr_6532.prdoc +++ /dev/null @@ -1,17 +0,0 @@ -# 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: Use frame umbrella crate in pallet-babe and pallet-staking-reward-curve - -doc: - - audience: Runtime Dev - description: | - Adapts the FRAME umbrella crate into pallet-tips, - adds the ensure macro and PalletId type commonly used in mock - runtimes accross different pallets to the prelude. - -crates: - - name: polkadot-sdk-frame - bump: minor - - name: pallet-tips - bump: patch \ No newline at end of file diff --git a/substrate/frame/src/lib.rs b/substrate/frame/src/lib.rs index aac170b86dd5..046fb0b4af0e 100644 --- a/substrate/frame/src/lib.rs +++ b/substrate/frame/src/lib.rs @@ -216,6 +216,9 @@ pub mod prelude { /// All hashing related things pub use super::hashing::*; + /// All arithmetic types used for safe math. + pub use super::arithmetic::*; + /// Runtime traits #[doc(no_inline)] pub use sp_runtime::traits::{ @@ -308,7 +311,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, ensure, + assert_storage_noop, ensure, storage_alias, }; pub use frame_system::{self, mocking::*}; @@ -496,8 +499,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::DispatchError; + pub use sp_runtime::{BuildStorage, DispatchError}; } } diff --git a/substrate/frame/tips/src/lib.rs b/substrate/frame/tips/src/lib.rs index 9ae3a7a4fa77..ba065aaf2b81 100644 --- a/substrate/frame/tips/src/lib.rs +++ b/substrate/frame/tips/src/lib.rs @@ -64,7 +64,6 @@ extern crate alloc; use alloc::{vec, vec::Vec}; use codec::{Decode, Encode}; use frame::{ - arithmetic::Percent, runtime::prelude::*, traits::{ AccountIdConversion, BadOrigin, ContainsLengthBound, Currency, diff --git a/substrate/frame/tips/src/tests.rs b/substrate/frame/tips/src/tests.rs index 94caa863fa9c..f27e4cbd6a60 100644 --- a/substrate/frame/tips/src/tests.rs +++ b/substrate/frame/tips/src/tests.rs @@ -21,7 +21,6 @@ use super::*; use crate::{self as pallet_tips, Event as TipEvent}; use frame::{ - arithmetic::{Perbill, Permill}, testing_prelude::*, traits::{ tokens::{PayFromAccount, UnityAssetBalanceConversion}, From 9020dc3c571b6acd05f2feb6b53cc002fe0681e5 Mon Sep 17 00:00:00 2001 From: bennethxyz Date: Mon, 30 Dec 2024 03:49:16 +0100 Subject: [PATCH 06/14] mainly prelude --- substrate/frame/src/lib.rs | 46 ++++++++++++++++++- substrate/frame/tips/src/benchmarking.rs | 2 +- substrate/frame/tips/src/lib.rs | 8 +--- .../tips/src/migrations/unreserve_deposits.rs | 6 +-- substrate/frame/tips/src/migrations/v4.rs | 25 ++++------ substrate/frame/tips/src/tests.rs | 15 ++---- 6 files changed, 60 insertions(+), 42 deletions(-) diff --git a/substrate/frame/src/lib.rs b/substrate/frame/src/lib.rs index 046fb0b4af0e..0b02dcae7902 100644 --- a/substrate/frame/src/lib.rs +++ b/substrate/frame/src/lib.rs @@ -219,6 +219,9 @@ pub mod prelude { /// All arithmetic types used for safe math. pub use super::arithmetic::*; + /// All account related things. + pub use super::account::*; + /// Runtime traits #[doc(no_inline)] pub use sp_runtime::traits::{ @@ -226,9 +229,21 @@ pub mod prelude { Saturating, StaticLookup, TrailingZeroInput, }; + /// Currency related traits. + pub use frame_support::traits::{ + tokens::{PayFromAccount, UnityAssetBalanceConversion}, + BalanceStatus::{self, Reserved}, + Currency, + ExistenceRequirement::KeepAlive, + LockableCurrency, OnUnbalanced, ReservableCurrency, + }; + /// Other error/result types for runtime #[doc(no_inline)] - pub use sp_runtime::{DispatchErrorWithPostInfo, DispatchResultWithInfo, TokenError}; + pub use sp_runtime::{ + DispatchError::{self, BadOrigin}, + DispatchErrorWithPostInfo, DispatchResultWithInfo, TokenError, + }; } #[cfg(any(feature = "try-runtime", test))] @@ -337,6 +352,11 @@ pub mod runtime { pub mod prelude { pub use crate::prelude::*; + /// All things runtime metadata. + pub use frame_support::traits::{ + CallMetadata, GetCallMetadata, PalletInfoAccess, STORAGE_VERSION_STORAGE_KEY_POSTFIX, + }; + /// All of the types related to the FRAME runtime executive. pub use frame_executive::*; @@ -356,6 +376,9 @@ pub mod runtime { /// sovereign account ID for a pallet. pub use frame_support::PalletId; + /// Runtime storage traits amd types. + pub use frame_support::storage::{migration::*, KeyPrefixIterator, StoragePrefixedMap}; + /// Macros to easily impl traits such as `Get` for types. // TODO: using linking in the Get in the line above triggers an ICE :/ pub use frame_support::{ord_parameter_types, parameter_types}; @@ -499,7 +522,7 @@ pub mod runtime { #[cfg(feature = "std")] pub mod testing_prelude { pub use sp_core::storage::Storage; - pub use sp_runtime::{BuildStorage, DispatchError}; + pub use sp_runtime::BuildStorage; } } @@ -538,6 +561,25 @@ pub mod hashing { pub use sp_runtime::traits::{BlakeTwo256, Hash, Keccak256}; } +/// Utility traits not tied to any direct(i.e. currency, account management e.t.c.) operation in the +/// runtime. +pub mod utility { + pub use frame_support::traits::{ + Everything, InsideBoth, InstanceFilter, VariantCount, VariantCountOf, + }; +} + +/// All account management related traits. +/// +/// This is already part of the [`prelude`]. +pub mod account { + pub use frame_support::traits::{ + ChangeMembers, ContainsLengthBound, EitherOfDiverse, InitializeMembers, NeverEnsureOrigin, + SortedMembers, + }; + pub use sp_runtime::traits::{AccountIdConversion, IdentifyAccount, IdentityLookup}; +} + /// Access to all of the dependencies of this crate. In case the prelude re-exports are not enough, /// this module can be used. /// diff --git a/substrate/frame/tips/src/benchmarking.rs b/substrate/frame/tips/src/benchmarking.rs index ca8b2bbabf06..8020d055d4d0 100644 --- a/substrate/frame/tips/src/benchmarking.rs +++ b/substrate/frame/tips/src/benchmarking.rs @@ -72,7 +72,7 @@ fn create_tips, I: 'static>( } Tips::::mutate(hash, |maybe_tip| { if let Some(open_tip) = maybe_tip { - open_tip.closes = Some(frame_system::pallet_prelude::BlockNumberFor::::zero()); + open_tip.closes = Some(BlockNumberFor::::zero()); } }); Ok(()) diff --git a/substrate/frame/tips/src/lib.rs b/substrate/frame/tips/src/lib.rs index ba065aaf2b81..7df6e874c1ef 100644 --- a/substrate/frame/tips/src/lib.rs +++ b/substrate/frame/tips/src/lib.rs @@ -63,13 +63,7 @@ pub mod weights; extern crate alloc; use alloc::{vec, vec::Vec}; use codec::{Decode, Encode}; -use frame::{ - runtime::prelude::*, - traits::{ - AccountIdConversion, BadOrigin, ContainsLengthBound, Currency, - ExistenceRequirement::KeepAlive, OnUnbalanced, ReservableCurrency, SortedMembers, - }, -}; +use frame::runtime::prelude::*; pub use pallet::*; pub use weights::WeightInfo; diff --git a/substrate/frame/tips/src/migrations/unreserve_deposits.rs b/substrate/frame/tips/src/migrations/unreserve_deposits.rs index 704b9f81166d..9e19d58fb875 100644 --- a/substrate/frame/tips/src/migrations/unreserve_deposits.rs +++ b/substrate/frame/tips/src/migrations/unreserve_deposits.rs @@ -20,11 +20,7 @@ use alloc::collections::btree_map::BTreeMap; use core::iter::Sum; -use frame::{ - runtime::prelude::*, - storage_alias, - traits::{Currency, LockableCurrency, ReservableCurrency}, -}; +use frame::testing_prelude::*; #[cfg(feature = "try-runtime")] const LOG_TARGET: &str = "runtime::tips::migrations::unreserve_deposits"; diff --git a/substrate/frame/tips/src/migrations/v4.rs b/substrate/frame/tips/src/migrations/v4.rs index 0a51ca13ed86..495094a97969 100644 --- a/substrate/frame/tips/src/migrations/v4.rs +++ b/substrate/frame/tips/src/migrations/v4.rs @@ -19,8 +19,7 @@ use super::super::LOG_TARGET; use core::str; use crate as pallet_tips; -use frame::{runtime::prelude::*, traits::STORAGE_VERSION_STORAGE_KEY_POSTFIX}; -use storage::StoragePrefixedMap as _; +use frame::runtime::prelude::*; /// Migrate the entire storage of this pallet to a new prefix. /// @@ -108,11 +107,10 @@ pub fn pre_migrate< let new_pallet_prefix = twox_128(new_pallet_name.as_bytes()); let storage_version_key = twox_128(STORAGE_VERSION_STORAGE_KEY_POSTFIX); - let mut new_pallet_prefix_iter = storage::KeyPrefixIterator::new( - new_pallet_prefix.to_vec(), - new_pallet_prefix.to_vec(), - |key| Ok(key.to_vec()), - ); + let mut new_pallet_prefix_iter = + KeyPrefixIterator::new(new_pallet_prefix.to_vec(), new_pallet_prefix.to_vec(), |key| { + Ok(key.to_vec()) + }); // Ensure nothing except the storage_version_key is stored in the new prefix. assert!(new_pallet_prefix_iter.all(|key| key == storage_version_key)); @@ -148,25 +146,20 @@ pub fn post_migrate< let old_pallet_prefix = twox_128(old_pallet_name.as_bytes()); let old_tips_key = [&old_pallet_prefix, &twox_128(storage_prefix_tips)[..]].concat(); let old_tips_key_iter = - storage::KeyPrefixIterator::new(old_tips_key.to_vec(), old_tips_key.to_vec(), |_| Ok(())); + KeyPrefixIterator::new(old_tips_key.to_vec(), old_tips_key.to_vec(), |_| Ok(())); assert_eq!(old_tips_key_iter.count(), 0); let old_reasons_key = [&old_pallet_prefix, &twox_128(storage_prefix_reasons)[..]].concat(); let old_reasons_key_iter = - storage::KeyPrefixIterator::new(old_reasons_key.to_vec(), old_reasons_key.to_vec(), |_| { - Ok(()) - }); + KeyPrefixIterator::new(old_reasons_key.to_vec(), old_reasons_key.to_vec(), |_| Ok(())); assert_eq!(old_reasons_key_iter.count(), 0); // Assert that the `Tips` and `Reasons` storages (if they exist) have been moved to the new // prefix. // NOTE: storage_version_key is already in the new prefix. let new_pallet_prefix = twox_128(new_pallet_name.as_bytes()); - let new_pallet_prefix_iter = storage::KeyPrefixIterator::new( - new_pallet_prefix.to_vec(), - new_pallet_prefix.to_vec(), - |_| Ok(()), - ); + let new_pallet_prefix_iter = + KeyPrefixIterator::new(new_pallet_prefix.to_vec(), new_pallet_prefix.to_vec(), |_| Ok(())); assert!(new_pallet_prefix_iter.count() >= 1); assert_eq!(

::on_chain_storage_version(), 4); diff --git a/substrate/frame/tips/src/tests.rs b/substrate/frame/tips/src/tests.rs index f27e4cbd6a60..191056e774bf 100644 --- a/substrate/frame/tips/src/tests.rs +++ b/substrate/frame/tips/src/tests.rs @@ -20,19 +20,12 @@ #![cfg(test)] use super::*; use crate::{self as pallet_tips, Event as TipEvent}; -use frame::{ - testing_prelude::*, - traits::{ - tokens::{PayFromAccount, UnityAssetBalanceConversion}, - IdentityLookup, NeverEnsureOrigin, PalletInfoAccess, - }, -}; -use storage::StoragePrefixedMap as _; +use frame::testing_prelude::*; #[cfg(feature = "try-runtime")] use crate::DispatchError::Other; -type Block = frame_system::mocking::MockBlock; +type Block = MockBlock; construct_runtime!( pub enum Test @@ -540,7 +533,7 @@ fn test_migration_v4() { TestExternalities::new(s).execute_with(|| { let old_pallet = "Treasury"; let new_pallet = ::name(); - storage::migration::move_pallet(new_pallet.as_bytes(), old_pallet.as_bytes()); + move_pallet(new_pallet.as_bytes(), old_pallet.as_bytes()); StorageVersion::new(0).put::(); crate::migrations::v4::pre_migrate::(old_pallet); @@ -551,7 +544,7 @@ fn test_migration_v4() { TestExternalities::new(Storage::default()).execute_with(|| { let old_pallet = "Treasury"; let new_pallet = ::name(); - storage::migration::move_pallet(new_pallet.as_bytes(), old_pallet.as_bytes()); + move_pallet(new_pallet.as_bytes(), old_pallet.as_bytes()); StorageVersion::new(0).put::(); crate::migrations::v4::pre_migrate::(old_pallet); From 64018ee1cb9bc179cc2a53ad50b2f54dd0c6a89b Mon Sep 17 00:00:00 2001 From: bennethxyz Date: Mon, 30 Dec 2024 05:33:31 +0100 Subject: [PATCH 07/14] nit migration prelude --- substrate/frame/tips/src/lib.rs | 19 ++++++++++--------- .../tips/src/migrations/unreserve_deposits.rs | 4 ++-- substrate/frame/tips/src/tests.rs | 13 +++---------- 3 files changed, 15 insertions(+), 21 deletions(-) diff --git a/substrate/frame/tips/src/lib.rs b/substrate/frame/tips/src/lib.rs index 7df6e874c1ef..b959ad2d3e16 100644 --- a/substrate/frame/tips/src/lib.rs +++ b/substrate/frame/tips/src/lib.rs @@ -63,11 +63,14 @@ pub mod weights; extern crate alloc; use alloc::{vec, vec::Vec}; use codec::{Decode, Encode}; -use frame::runtime::prelude::*; +use frame::prelude::*; pub use pallet::*; pub use weights::WeightInfo; +#[cfg(feature = "try-runtime")] +use frame::try_runtime::TryRuntimeError; + const LOG_TARGET: &str = "runtime::tips"; pub type BalanceOf = pallet_treasury::BalanceOf; @@ -464,7 +467,7 @@ pub mod pallet { } #[cfg(feature = "try-runtime")] - fn try_state(_n: BlockNumberFor) -> Result<(), frame::try_runtime::TryRuntimeError> { + fn try_state(_n: BlockNumberFor) -> Result<(), TryRuntimeError> { Self::do_try_state() } } @@ -633,16 +636,14 @@ impl, I: 'static> Pallet { /// 1. The number of entries in `Tips` should be equal to `Reasons`. /// 2. Reasons exists for each Tip[`OpenTip.reason`]. /// 3. If `OpenTip.finders_fee` is true, then OpenTip.deposit should be greater than zero. - #[cfg(feature = "try-runtime")] - pub fn do_try_state() -> Result<(), frame::try_runtime::TryRuntimeError> { + #[cfg(any(feature = "try-runtime", test))] + pub fn do_try_state() -> Result<(), TryRuntimeError> { let reasons = Reasons::::iter_keys().collect::>(); let tips = Tips::::iter_keys().collect::>(); ensure!( reasons.len() == tips.len(), - frame::try_runtime::TryRuntimeError::Other( - "Equal length of entries in `Tips` and `Reasons` Storage" - ) + TryRuntimeError::Other("Equal length of entries in `Tips` and `Reasons` Storage") ); for tip in Tips::::iter_keys() { @@ -651,7 +652,7 @@ impl, I: 'static> Pallet { if open_tip.finders_fee { ensure!( !open_tip.deposit.is_zero(), - frame::try_runtime::TryRuntimeError::Other( + TryRuntimeError::Other( "Tips with `finders_fee` should have non-zero `deposit`." ) ) @@ -659,7 +660,7 @@ impl, I: 'static> Pallet { ensure!( reasons.contains(&open_tip.reason), - frame::try_runtime::TryRuntimeError::Other("no reason for this tip") + TryRuntimeError::Other("no reason for this tip") ); } Ok(()) diff --git a/substrate/frame/tips/src/migrations/unreserve_deposits.rs b/substrate/frame/tips/src/migrations/unreserve_deposits.rs index 9e19d58fb875..164e19e31284 100644 --- a/substrate/frame/tips/src/migrations/unreserve_deposits.rs +++ b/substrate/frame/tips/src/migrations/unreserve_deposits.rs @@ -20,7 +20,7 @@ use alloc::collections::btree_map::BTreeMap; use core::iter::Sum; -use frame::testing_prelude::*; +use frame::runtime::prelude::*; #[cfg(feature = "try-runtime")] const LOG_TARGET: &str = "runtime::tips::migrations::unreserve_deposits"; @@ -57,7 +57,7 @@ pub trait UnlockConfig: 'static { /// An open tipping "motion". Retains all details of a tip including information on the finder /// and the members who have voted. -#[storage_alias(dynamic)] +#[frame::storage_alias(dynamic)] type Tips, I: 'static> = StorageMap< >::PalletName, Twox64Concat, diff --git a/substrate/frame/tips/src/tests.rs b/substrate/frame/tips/src/tests.rs index 191056e774bf..606d58034994 100644 --- a/substrate/frame/tips/src/tests.rs +++ b/substrate/frame/tips/src/tests.rs @@ -22,9 +22,6 @@ use super::*; use crate::{self as pallet_tips, Event as TipEvent}; use frame::testing_prelude::*; -#[cfg(feature = "try-runtime")] -use crate::DispatchError::Other; - type Block = MockBlock; construct_runtime!( @@ -181,7 +178,6 @@ pub fn new_test_ext() -> TestExternalities { pub fn build_and_execute(test: impl FnOnce() -> ()) { new_test_ext().execute_with(|| { test(); - #[cfg(feature = "try-runtime")] Tips::do_try_state().expect("All invariants must hold after a test"); }); } @@ -631,10 +627,9 @@ fn equal_entries_invariant() { pallet_tips::Tips::::insert(hash1, tip); // Invariant violated - #[cfg(feature = "try-runtime")] assert_eq!( Tips::do_try_state(), - Err(Other("Equal length of entries in `Tips` and `Reasons` Storage")) + Err(TryRuntimeError::Other("Equal length of entries in `Tips` and `Reasons` Storage")) ); }) } @@ -650,10 +645,9 @@ fn finders_fee_invariant() { assert_ok!(Tips::report_awesome(RuntimeOrigin::signed(0), b"".to_vec(), 3)); // Invariant violated - #[cfg(feature = "try-runtime")] assert_eq!( Tips::do_try_state(), - Err(Other("Tips with `finders_fee` should have non-zero `deposit`.")) + Err(TryRuntimeError::Other("Tips with `finders_fee` should have non-zero `deposit`.")) ); }) } @@ -675,8 +669,7 @@ fn reasons_invariant() { pallet_tips::Tips::::insert(hash[0], open_tip); // Invariant violated - #[cfg(feature = "try-runtime")] - assert_eq!(Tips::do_try_state(), Err(Other("no reason for this tip"))); + assert_eq!(Tips::do_try_state(), Err(TryRuntimeError::Other("no reason for this tip"))); }) } From 17ebc083dc660c984fa93434f1789a8ac279bdf8 Mon Sep 17 00:00:00 2001 From: bennethxyz Date: Mon, 30 Dec 2024 05:52:35 +0100 Subject: [PATCH 08/14] tryruntime unreserve deposit migration --- substrate/frame/tips/src/migrations/unreserve_deposits.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/substrate/frame/tips/src/migrations/unreserve_deposits.rs b/substrate/frame/tips/src/migrations/unreserve_deposits.rs index 164e19e31284..270d632e4fc6 100644 --- a/substrate/frame/tips/src/migrations/unreserve_deposits.rs +++ b/substrate/frame/tips/src/migrations/unreserve_deposits.rs @@ -22,6 +22,9 @@ use alloc::collections::btree_map::BTreeMap; use core::iter::Sum; use frame::runtime::prelude::*; +#[cfg(feature = "try-runtime")] +use frame::try_runtime::TryRuntimeError; + #[cfg(feature = "try-runtime")] const LOG_TARGET: &str = "runtime::tips::migrations::unreserve_deposits"; @@ -124,7 +127,7 @@ where /// Fails with a `TryRuntimeError` if somehow the amount reserved by this pallet is greater than /// the actual total reserved amount for any accounts. #[cfg(feature = "try-runtime")] - fn pre_upgrade() -> Result, frame::try_runtime::TryRuntimeError> { + fn pre_upgrade() -> Result, TryRuntimeError> { use codec::Encode; // Get the Tips pallet view of balances it has reserved @@ -178,7 +181,7 @@ where #[cfg(feature = "try-runtime")] fn post_upgrade( account_reserved_before_bytes: alloc::vec::Vec, - ) -> Result<(), frame::try_runtime::TryRuntimeError> { + ) -> Result<(), TryRuntimeError> { use codec::Decode; let account_reserved_before = BTreeMap::>::decode( From 5b528c3ae57117e58574c9ed62a5cb60c3a6ad82 Mon Sep 17 00:00:00 2001 From: bennethxyz Date: Mon, 30 Dec 2024 06:06:06 +0100 Subject: [PATCH 09/14] nit --- substrate/frame/tips/src/migrations/v4.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/substrate/frame/tips/src/migrations/v4.rs b/substrate/frame/tips/src/migrations/v4.rs index 495094a97969..58c7cb9c57db 100644 --- a/substrate/frame/tips/src/migrations/v4.rs +++ b/substrate/frame/tips/src/migrations/v4.rs @@ -53,7 +53,7 @@ pub fn migrate::storage_prefix(); - storage::migration::move_storage_from_pallet( + move_storage_from_pallet( storage_prefix, old_pallet_name.as_bytes(), new_pallet_name.as_bytes(), @@ -61,7 +61,7 @@ pub fn migrate::storage_prefix(); - storage::migration::move_storage_from_pallet( + move_storage_from_pallet( storage_prefix, old_pallet_name.as_bytes(), new_pallet_name.as_bytes(), From aae98122954c9106cdb80efbe05a9e4f7676568d Mon Sep 17 00:00:00 2001 From: bennethxyz Date: Mon, 30 Dec 2024 06:08:37 +0100 Subject: [PATCH 10/14] comment nit --- substrate/frame/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/substrate/frame/src/lib.rs b/substrate/frame/src/lib.rs index 0b02dcae7902..4f90253f93ec 100644 --- a/substrate/frame/src/lib.rs +++ b/substrate/frame/src/lib.rs @@ -561,7 +561,7 @@ pub mod hashing { pub use sp_runtime::traits::{BlakeTwo256, Hash, Keccak256}; } -/// Utility traits not tied to any direct(i.e. currency, account management e.t.c.) operation in the +/// Utility traits not tied to any direct operation(i.e. currency, account management e.t.c.) in the /// runtime. pub mod utility { pub use frame_support::traits::{ From 6ed7aa0eda16dc4cd4986f9f901906bd200539ea Mon Sep 17 00:00:00 2001 From: bennethxyz Date: Mon, 30 Dec 2024 13:02:30 +0100 Subject: [PATCH 11/14] account mod before utility arrangement nit --- substrate/frame/src/lib.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/substrate/frame/src/lib.rs b/substrate/frame/src/lib.rs index 4f90253f93ec..a31a95d465ff 100644 --- a/substrate/frame/src/lib.rs +++ b/substrate/frame/src/lib.rs @@ -561,14 +561,6 @@ pub mod hashing { pub use sp_runtime::traits::{BlakeTwo256, Hash, Keccak256}; } -/// Utility traits not tied to any direct operation(i.e. currency, account management e.t.c.) in the -/// runtime. -pub mod utility { - pub use frame_support::traits::{ - Everything, InsideBoth, InstanceFilter, VariantCount, VariantCountOf, - }; -} - /// All account management related traits. /// /// This is already part of the [`prelude`]. @@ -580,6 +572,14 @@ pub mod account { pub use sp_runtime::traits::{AccountIdConversion, IdentifyAccount, IdentityLookup}; } +/// Utility traits not tied to any direct operation(i.e. currency, account management e.t.c.) in the +/// runtime. +pub mod utility { + pub use frame_support::traits::{ + Everything, InsideBoth, InstanceFilter, VariantCount, VariantCountOf, + }; +} + /// Access to all of the dependencies of this crate. In case the prelude re-exports are not enough, /// this module can be used. /// From a210e1b37a69cae5ed9479fe653a49a6a7995783 Mon Sep 17 00:00:00 2001 From: bennethxyz Date: Mon, 30 Dec 2024 14:05:44 +0100 Subject: [PATCH 12/14] TypedGet in scope --- substrate/frame/tips/src/migrations/unreserve_deposits.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/substrate/frame/tips/src/migrations/unreserve_deposits.rs b/substrate/frame/tips/src/migrations/unreserve_deposits.rs index 270d632e4fc6..6d41e5c4d91d 100644 --- a/substrate/frame/tips/src/migrations/unreserve_deposits.rs +++ b/substrate/frame/tips/src/migrations/unreserve_deposits.rs @@ -233,7 +233,7 @@ mod test { type Currency = Balances; type TipReportDepositBase = ConstU64<1>; type DataDepositPerByte = ConstU64<1>; - type Hash = crate::H256; + type Hash = H256; type AccountId = u128; type BlockNumber = BlockNumberFor; type DbWeight = (); @@ -277,8 +277,8 @@ mod test { ::Currency::reserved_balance(&tipper_0), tipper_0_initial_reserved + <::TipReportDepositBase as TypedGet>::get() + - <::DataDepositPerByte as frame::prelude::TypedGet>::get( - ) * tip_0_reason.len() as u64 + <::DataDepositPerByte as TypedGet>::get() * + tip_0_reason.len() as u64 ); assert_eq!( ::Currency::reserved_balance(&tipper_1), From be9e0dc20748b6951a18c23a8e1ac39e0d544ab4 Mon Sep 17 00:00:00 2001 From: bennethxyz Date: Sat, 4 Jan 2025 11:24:41 +0100 Subject: [PATCH 13/14] not utility, not currency --- substrate/frame/src/lib.rs | 22 ++++--------------- substrate/frame/tips/src/lib.rs | 13 ++++++----- .../tips/src/migrations/unreserve_deposits.rs | 5 ++++- substrate/frame/tips/src/tests.rs | 9 +++++++- 4 files changed, 24 insertions(+), 25 deletions(-) diff --git a/substrate/frame/src/lib.rs b/substrate/frame/src/lib.rs index a31a95d465ff..be3d004062a9 100644 --- a/substrate/frame/src/lib.rs +++ b/substrate/frame/src/lib.rs @@ -203,7 +203,10 @@ 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, IsSubType, OnRuntimeUpgrade}; + pub use frame_support::traits::{ + Contains, Everything, InsideBoth, InstanceFilter, IsSubType, OnRuntimeUpgrade, + VariantCount, VariantCountOf, + }; /// Pallet prelude of `frame-system`. #[doc(no_inline)] @@ -229,15 +232,6 @@ pub mod prelude { Saturating, StaticLookup, TrailingZeroInput, }; - /// Currency related traits. - pub use frame_support::traits::{ - tokens::{PayFromAccount, UnityAssetBalanceConversion}, - BalanceStatus::{self, Reserved}, - Currency, - ExistenceRequirement::KeepAlive, - LockableCurrency, OnUnbalanced, ReservableCurrency, - }; - /// Other error/result types for runtime #[doc(no_inline)] pub use sp_runtime::{ @@ -572,14 +566,6 @@ pub mod account { pub use sp_runtime::traits::{AccountIdConversion, IdentifyAccount, IdentityLookup}; } -/// Utility traits not tied to any direct operation(i.e. currency, account management e.t.c.) in the -/// runtime. -pub mod utility { - pub use frame_support::traits::{ - Everything, InsideBoth, InstanceFilter, VariantCount, VariantCountOf, - }; -} - /// Access to all of the dependencies of this crate. In case the prelude re-exports are not enough, /// this module can be used. /// diff --git a/substrate/frame/tips/src/lib.rs b/substrate/frame/tips/src/lib.rs index b959ad2d3e16..8eebbbb91e46 100644 --- a/substrate/frame/tips/src/lib.rs +++ b/substrate/frame/tips/src/lib.rs @@ -63,14 +63,17 @@ pub mod weights; extern crate alloc; use alloc::{vec, vec::Vec}; use codec::{Decode, Encode}; -use frame::prelude::*; - -pub use pallet::*; -pub use weights::WeightInfo; +use frame::{ + prelude::*, + traits::{Currency, ExistenceRequirement::KeepAlive, OnUnbalanced, ReservableCurrency}, +}; #[cfg(feature = "try-runtime")] use frame::try_runtime::TryRuntimeError; +pub use pallet::*; +pub use weights::WeightInfo; + const LOG_TARGET: &str = "runtime::tips"; pub type BalanceOf = pallet_treasury::BalanceOf; @@ -636,7 +639,7 @@ impl, I: 'static> Pallet { /// 1. The number of entries in `Tips` should be equal to `Reasons`. /// 2. Reasons exists for each Tip[`OpenTip.reason`]. /// 3. If `OpenTip.finders_fee` is true, then OpenTip.deposit should be greater than zero. - #[cfg(any(feature = "try-runtime", test))] + #[cfg(feature = "try-runtime")] pub fn do_try_state() -> Result<(), TryRuntimeError> { let reasons = Reasons::::iter_keys().collect::>(); let tips = Tips::::iter_keys().collect::>(); diff --git a/substrate/frame/tips/src/migrations/unreserve_deposits.rs b/substrate/frame/tips/src/migrations/unreserve_deposits.rs index 6d41e5c4d91d..c0ecdbd32cb0 100644 --- a/substrate/frame/tips/src/migrations/unreserve_deposits.rs +++ b/substrate/frame/tips/src/migrations/unreserve_deposits.rs @@ -20,7 +20,10 @@ use alloc::collections::btree_map::BTreeMap; use core::iter::Sum; -use frame::runtime::prelude::*; +use frame::{ + runtime::prelude::*, + traits::{Currency, LockableCurrency, ReservableCurrency}, +}; #[cfg(feature = "try-runtime")] use frame::try_runtime::TryRuntimeError; diff --git a/substrate/frame/tips/src/tests.rs b/substrate/frame/tips/src/tests.rs index 606d58034994..2faafbe6aa4e 100644 --- a/substrate/frame/tips/src/tests.rs +++ b/substrate/frame/tips/src/tests.rs @@ -20,7 +20,10 @@ #![cfg(test)] use super::*; use crate::{self as pallet_tips, Event as TipEvent}; -use frame::testing_prelude::*; +use frame::{ + testing_prelude::*, + traits::tokens::{PayFromAccount, UnityAssetBalanceConversion}, +}; type Block = MockBlock; @@ -178,6 +181,7 @@ pub fn new_test_ext() -> TestExternalities { pub fn build_and_execute(test: impl FnOnce() -> ()) { new_test_ext().execute_with(|| { test(); + #[cfg(feature = "try-runtime")] Tips::do_try_state().expect("All invariants must hold after a test"); }); } @@ -604,6 +608,7 @@ fn report_awesome_and_tip_works_second_instance() { } #[test] +#[cfg(feature = "try-runtime")] fn equal_entries_invariant() { new_test_ext().execute_with(|| { Balances::make_free_balance_be(&Treasury::account_id(), 101); @@ -635,6 +640,7 @@ fn equal_entries_invariant() { } #[test] +#[cfg(feature = "try-runtime")] fn finders_fee_invariant() { new_test_ext().execute_with(|| { // Breaks invariant by having a zero deposit. @@ -653,6 +659,7 @@ fn finders_fee_invariant() { } #[test] +#[cfg(feature = "try-runtime")] fn reasons_invariant() { new_test_ext().execute_with(|| { Balances::make_free_balance_be(&Treasury::account_id(), 101); From 84ada288cd6b976fe4788ef73a8c3a0d703b7cf2 Mon Sep 17 00:00:00 2001 From: bennethxyz Date: Sat, 4 Jan 2025 12:11:29 +0100 Subject: [PATCH 14/14] cargo fmt --- substrate/frame/src/lib.rs | 7 ++++--- substrate/frame/tips/src/migrations/unreserve_deposits.rs | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/substrate/frame/src/lib.rs b/substrate/frame/src/lib.rs index 798e80fd1343..f05549e4d79f 100644 --- a/substrate/frame/src/lib.rs +++ b/substrate/frame/src/lib.rs @@ -204,8 +204,8 @@ pub mod prelude { #[doc(no_inline)] pub use frame_support::dispatch::{GetDispatchInfo, PostDispatchInfo}; pub use frame_support::traits::{ - Contains, EstimateNextSessionRotation, Everything, InsideBoth, InstanceFilter, IsSubType, OnRuntimeUpgrade, - VariantCount, VariantCountOf, OneSessionHandler, + Contains, EstimateNextSessionRotation, Everything, InsideBoth, InstanceFilter, IsSubType, + OnRuntimeUpgrade, OneSessionHandler, VariantCount, VariantCountOf, }; /// Pallet prelude of `frame-system`. @@ -238,7 +238,8 @@ pub mod prelude { /// Other runtime types and traits #[doc(no_inline)] - pub use sp_runtime::{BoundToRuntimeAppPublic, + pub use sp_runtime::{ + BoundToRuntimeAppPublic, DispatchError::{self, BadOrigin}, DispatchErrorWithPostInfo, DispatchResultWithInfo, TokenError, }; diff --git a/substrate/frame/tips/src/migrations/unreserve_deposits.rs b/substrate/frame/tips/src/migrations/unreserve_deposits.rs index c0ecdbd32cb0..31cf57c5c6f9 100644 --- a/substrate/frame/tips/src/migrations/unreserve_deposits.rs +++ b/substrate/frame/tips/src/migrations/unreserve_deposits.rs @@ -53,7 +53,7 @@ pub trait UnlockConfig: 'static { /// Should match the currency type previously used for the pallet, if applicable. type DataDepositPerByte: Get>; /// The name of the pallet as previously configured in - /// [`construct_runtime!`](frame_support::construct_runtime). + /// [`construct_runtime!`](construct_runtime). type PalletName: Get<&'static str>; /// The DB weight as configured in the runtime to calculate the correct weight. type DbWeight: Get;