From 0b3d7607d2f78e9b62d92ccf487f0984f921043e Mon Sep 17 00:00:00 2001 From: Parth Mittal <76661350+mittal-parth@users.noreply.github.com> Date: Tue, 16 Jul 2024 14:23:37 +0530 Subject: [PATCH] Remove `pallet-getter` usage from pallet-session (#4972) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As per #3326, removes usage of the `pallet::getter` macro from the `session` pallet. The syntax `StorageItem::::get()` should be used instead. Also, adds public functions for compatibility. NOTE: The `./historical` directory has not been modified. cc @muraca polkadot address: 5GsLutpKjbzsbTphebs9Uy4YK6gTN47MAaz6njPktidjR5cp --------- Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> Co-authored-by: Bastian Köcher --- prdoc/pr_4972.prdoc | 14 ++++ substrate/frame/session/README.md | 2 +- .../frame/session/benchmarking/src/inner.rs | 2 +- substrate/frame/session/src/lib.rs | 74 +++++++++++-------- substrate/frame/session/src/tests.rs | 44 +++++------ 5 files changed, 83 insertions(+), 53 deletions(-) create mode 100644 prdoc/pr_4972.prdoc diff --git a/prdoc/pr_4972.prdoc b/prdoc/pr_4972.prdoc new file mode 100644 index 000000000000..dd9f1b531aad --- /dev/null +++ b/prdoc/pr_4972.prdoc @@ -0,0 +1,14 @@ +# 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: "Remove `pallet::getter` usage from pallet-session" + +doc: + - audience: Runtime Dev + description: | + This PR removes the `pallet::getter`s from `pallet-session`. + The syntax `StorageItem::::get()` should be used instead. + +crates: + - name: pallet-session + bump: minor diff --git a/substrate/frame/session/README.md b/substrate/frame/session/README.md index fa7c9b3f9834..5a063bffee0b 100644 --- a/substrate/frame/session/README.md +++ b/substrate/frame/session/README.md @@ -70,7 +70,7 @@ set. use pallet_session as session; fn validators() -> Vec<::ValidatorId> { - >::validators() + pallet_session::Validators::::get() } ``` diff --git a/substrate/frame/session/benchmarking/src/inner.rs b/substrate/frame/session/benchmarking/src/inner.rs index f08e10f07869..9ba47b34ed7a 100644 --- a/substrate/frame/session/benchmarking/src/inner.rs +++ b/substrate/frame/session/benchmarking/src/inner.rs @@ -152,7 +152,7 @@ fn check_membership_proof_setup( Pallet::::on_initialize(frame_system::pallet_prelude::BlockNumberFor::::one()); // skip sessions until the new validator set is enacted - while Session::::validators().len() < n as usize { + while Validators::::get().len() < n as usize { Session::::rotate_session(); } diff --git a/substrate/frame/session/src/lib.rs b/substrate/frame/session/src/lib.rs index 45f70dfa2582..e1a2a31911fe 100644 --- a/substrate/frame/session/src/lib.rs +++ b/substrate/frame/session/src/lib.rs @@ -95,7 +95,7 @@ //! use pallet_session as session; //! //! fn validators() -> Vec<::ValidatorId> { -//! >::validators() +//! pallet_session::Validators::::get() //! } //! # fn main(){} //! ``` @@ -447,7 +447,7 @@ pub mod pallet { }); for (account, val, keys) in self.keys.iter().cloned() { - >::inner_set_keys(&val, keys) + Pallet::::inner_set_keys(&val, keys) .expect("genesis config must not contain duplicates; qed"); if frame_system::Pallet::::inc_consumers_without_limit(&account).is_err() { // This will leak a provider reference, however it only happens once (at @@ -479,7 +479,7 @@ pub mod pallet { T::SessionHandler::on_genesis_session::(&queued_keys); Validators::::put(initial_validators_0); - >::put(queued_keys); + QueuedKeys::::put(queued_keys); T::SessionManager::start_session(0); } @@ -487,12 +487,10 @@ pub mod pallet { /// The current set of validators. #[pallet::storage] - #[pallet::getter(fn validators)] pub type Validators = StorageValue<_, Vec, ValueQuery>; /// Current index of the session. #[pallet::storage] - #[pallet::getter(fn current_index)] pub type CurrentIndex = StorageValue<_, SessionIndex, ValueQuery>; /// True if the underlying economic identities or weighting behind the validators @@ -503,7 +501,6 @@ pub mod pallet { /// The queued keys for the next session. When the next session begins, these keys /// will be used to determine the validator's session keys. #[pallet::storage] - #[pallet::getter(fn queued_keys)] pub type QueuedKeys = StorageValue<_, Vec<(T::ValidatorId, T::Keys)>, ValueQuery>; /// Indices of disabled validators. @@ -512,7 +509,6 @@ pub mod pallet { /// disabled using binary search. It gets cleared when `on_session_ending` returns /// a new set of identities. #[pallet::storage] - #[pallet::getter(fn disabled_validators)] pub type DisabledValidators = StorageValue<_, Vec, ValueQuery>; /// The next session keys for a validator. @@ -609,33 +605,53 @@ pub mod pallet { } impl Pallet { + /// Public function to access the current set of validators. + pub fn validators() -> Vec { + Validators::::get() + } + + /// Public function to access the current session index. + pub fn current_index() -> SessionIndex { + CurrentIndex::::get() + } + + /// Public function to access the queued keys. + pub fn queued_keys() -> Vec<(T::ValidatorId, T::Keys)> { + QueuedKeys::::get() + } + + /// Public function to access the disabled validators. + pub fn disabled_validators() -> Vec { + DisabledValidators::::get() + } + /// Move on to next session. Register new validator set and session keys. Changes to the /// validator set have a session of delay to take effect. This allows for equivocation /// punishment after a fork. pub fn rotate_session() { - let session_index = >::get(); + let session_index = CurrentIndex::::get(); log::trace!(target: "runtime::session", "rotating session {:?}", session_index); - let changed = >::get(); + let changed = QueuedChanged::::get(); // Inform the session handlers that a session is going to end. T::SessionHandler::on_before_session_ending(); T::SessionManager::end_session(session_index); // Get queued session keys and validators. - let session_keys = >::get(); + let session_keys = QueuedKeys::::get(); let validators = session_keys.iter().map(|(validator, _)| validator.clone()).collect::>(); Validators::::put(&validators); if changed { // reset disabled validators if active set was changed - >::take(); + DisabledValidators::::take(); } // Increment session index. let session_index = session_index + 1; - >::put(session_index); + CurrentIndex::::put(session_index); T::SessionManager::start_session(session_index); @@ -683,8 +699,8 @@ impl Pallet { (queued_amalgamated, changed) }; - >::put(queued_amalgamated.clone()); - >::put(next_changed); + QueuedKeys::::put(queued_amalgamated.clone()); + QueuedChanged::::put(next_changed); // Record that this happened. Self::deposit_event(Event::NewSession { session_index }); @@ -699,7 +715,7 @@ impl Pallet { return false } - >::mutate(|disabled| { + DisabledValidators::::mutate(|disabled| { if let Err(index) = disabled.binary_search(&i) { disabled.insert(index, i); T::SessionHandler::on_disabled(i); @@ -716,7 +732,7 @@ impl Pallet { /// Returns `false` either if the validator could not be found or it was already /// disabled. pub fn disable(c: &T::ValidatorId) -> bool { - Self::validators() + Validators::::get() .iter() .position(|i| i == c) .map(|i| Self::disable_index(i as u32)) @@ -747,7 +763,7 @@ impl Pallet { let new_ids = T::Keys::key_ids(); // Translate NextKeys, and key ownership relations at the same time. - >::translate::(|val, old_keys| { + NextKeys::::translate::(|val, old_keys| { // Clear all key ownership relations. Typically the overlap should // stay the same, but no guarantees by the upgrade function. for i in old_ids.iter() { @@ -764,7 +780,7 @@ impl Pallet { Some(new_keys) }); - let _ = >::translate::, _>(|k| { + let _ = QueuedKeys::::translate::, _>(|k| { k.map(|k| { k.into_iter() .map(|(val, old_keys)| (val.clone(), upgrade(val, old_keys))) @@ -850,28 +866,28 @@ impl Pallet { } fn load_keys(v: &T::ValidatorId) -> Option { - >::get(v) + NextKeys::::get(v) } fn take_keys(v: &T::ValidatorId) -> Option { - >::take(v) + NextKeys::::take(v) } fn put_keys(v: &T::ValidatorId, keys: &T::Keys) { - >::insert(v, keys); + NextKeys::::insert(v, keys); } /// Query the owner of a session key by returning the owner's validator ID. pub fn key_owner(id: KeyTypeId, key_data: &[u8]) -> Option { - >::get((id, key_data)) + KeyOwner::::get((id, key_data)) } fn put_key_owner(id: KeyTypeId, key_data: &[u8], v: &T::ValidatorId) { - >::insert((id, key_data), v) + KeyOwner::::insert((id, key_data), v) } fn clear_key_owner(id: KeyTypeId, key_data: &[u8]) { - >::remove((id, key_data)); + KeyOwner::::remove((id, key_data)); } } @@ -886,11 +902,11 @@ impl ValidatorSet for Pallet { type ValidatorIdOf = T::ValidatorIdOf; fn session_index() -> sp_staking::SessionIndex { - Pallet::::current_index() + CurrentIndex::::get() } fn validators() -> Vec { - Pallet::::validators() + Validators::::get() } } @@ -908,11 +924,11 @@ impl EstimateNextNewSession> for Pallet { impl frame_support::traits::DisabledValidators for Pallet { fn is_disabled(index: u32) -> bool { - >::disabled_validators().binary_search(&index).is_ok() + DisabledValidators::::get().binary_search(&index).is_ok() } fn disabled_validators() -> Vec { - >::disabled_validators() + DisabledValidators::::get() } } @@ -930,7 +946,7 @@ impl> FindAuthor { let i = Inner::find_author(digests)?; - let validators = >::validators(); + let validators = Validators::::get(); validators.get(i as usize).cloned() } } diff --git a/substrate/frame/session/src/tests.rs b/substrate/frame/session/src/tests.rs index 69337e016ea8..f392c2ab7663 100644 --- a/substrate/frame/session/src/tests.rs +++ b/substrate/frame/session/src/tests.rs @@ -44,7 +44,7 @@ fn initialize_block(block: u64) { fn simple_setup_should_work() { new_test_ext().execute_with(|| { assert_eq!(authorities(), vec![UintAuthorityId(1), UintAuthorityId(2), UintAuthorityId(3)]); - assert_eq!(Session::validators(), vec![1, 2, 3]); + assert_eq!(Validators::::get(), vec![1, 2, 3]); }); } @@ -60,7 +60,7 @@ fn put_get_keys() { fn keys_cleared_on_kill() { let mut ext = new_test_ext(); ext.execute_with(|| { - assert_eq!(Session::validators(), vec![1, 2, 3]); + assert_eq!(Validators::::get(), vec![1, 2, 3]); assert_eq!(Session::load_keys(&1), Some(UintAuthorityId(1).into())); let id = DUMMY; @@ -79,7 +79,7 @@ fn keys_cleared_on_kill() { fn purge_keys_works_for_stash_id() { let mut ext = new_test_ext(); ext.execute_with(|| { - assert_eq!(Session::validators(), vec![1, 2, 3]); + assert_eq!(Validators::::get(), vec![1, 2, 3]); TestValidatorIdOf::set(vec![(10, 1), (20, 2), (3, 3)].into_iter().collect()); assert_eq!(Session::load_keys(&1), Some(UintAuthorityId(1).into())); assert_eq!(Session::load_keys(&2), Some(UintAuthorityId(2).into())); @@ -108,10 +108,10 @@ fn authorities_should_track_validators() { force_new_session(); initialize_block(1); assert_eq!( - Session::queued_keys(), + QueuedKeys::::get(), vec![(1, UintAuthorityId(1).into()), (2, UintAuthorityId(2).into()),] ); - assert_eq!(Session::validators(), vec![1, 2, 3]); + assert_eq!(Validators::::get(), vec![1, 2, 3]); assert_eq!(authorities(), vec![UintAuthorityId(1), UintAuthorityId(2), UintAuthorityId(3)]); assert!(before_session_end_called()); reset_before_session_end_called(); @@ -119,10 +119,10 @@ fn authorities_should_track_validators() { force_new_session(); initialize_block(2); assert_eq!( - Session::queued_keys(), + QueuedKeys::::get(), vec![(1, UintAuthorityId(1).into()), (2, UintAuthorityId(2).into()),] ); - assert_eq!(Session::validators(), vec![1, 2]); + assert_eq!(Validators::::get(), vec![1, 2]); assert_eq!(authorities(), vec![UintAuthorityId(1), UintAuthorityId(2)]); assert!(before_session_end_called()); reset_before_session_end_called(); @@ -132,28 +132,28 @@ fn authorities_should_track_validators() { force_new_session(); initialize_block(3); assert_eq!( - Session::queued_keys(), + QueuedKeys::::get(), vec![ (1, UintAuthorityId(1).into()), (2, UintAuthorityId(2).into()), (4, UintAuthorityId(4).into()), ] ); - assert_eq!(Session::validators(), vec![1, 2]); + assert_eq!(Validators::::get(), vec![1, 2]); assert_eq!(authorities(), vec![UintAuthorityId(1), UintAuthorityId(2)]); assert!(before_session_end_called()); force_new_session(); initialize_block(4); assert_eq!( - Session::queued_keys(), + QueuedKeys::::get(), vec![ (1, UintAuthorityId(1).into()), (2, UintAuthorityId(2).into()), (4, UintAuthorityId(4).into()), ] ); - assert_eq!(Session::validators(), vec![1, 2, 4]); + assert_eq!(Validators::::get(), vec![1, 2, 4]); assert_eq!(authorities(), vec![UintAuthorityId(1), UintAuthorityId(2), UintAuthorityId(4)]); }); } @@ -164,20 +164,20 @@ fn should_work_with_early_exit() { set_session_length(10); initialize_block(1); - assert_eq!(Session::current_index(), 0); + assert_eq!(CurrentIndex::::get(), 0); initialize_block(2); - assert_eq!(Session::current_index(), 0); + assert_eq!(CurrentIndex::::get(), 0); force_new_session(); initialize_block(3); - assert_eq!(Session::current_index(), 1); + assert_eq!(CurrentIndex::::get(), 1); initialize_block(9); - assert_eq!(Session::current_index(), 1); + assert_eq!(CurrentIndex::::get(), 1); initialize_block(10); - assert_eq!(Session::current_index(), 2); + assert_eq!(CurrentIndex::::get(), 2); }); } @@ -402,7 +402,7 @@ fn upgrade_keys() { // Set `QueuedKeys`. { - let storage_key = >::hashed_key(); + let storage_key = super::QueuedKeys::::hashed_key(); assert!(storage::unhashed::exists(&storage_key)); storage::unhashed::put(&storage_key, &val_keys); } @@ -410,7 +410,7 @@ fn upgrade_keys() { // Set `NextKeys`. { for &(i, ref keys) in val_keys.iter() { - let storage_key = >::hashed_key_for(i); + let storage_key = super::NextKeys::::hashed_key_for(i); assert!(storage::unhashed::exists(&storage_key)); storage::unhashed::put(&storage_key, keys); } @@ -446,12 +446,12 @@ fn upgrade_keys() { // Check queued keys. assert_eq!( - Session::queued_keys(), + QueuedKeys::::get(), vec![(1, mock_keys_for(1)), (2, mock_keys_for(2)), (3, mock_keys_for(3)),], ); for i in 1u64..4 { - assert_eq!(>::get(&i), Some(mock_keys_for(i))); + assert_eq!(super::NextKeys::::get(&i), Some(mock_keys_for(i))); } }) } @@ -466,8 +466,8 @@ fn test_migration_v1() { use frame_support::traits::{PalletInfoAccess, StorageVersion}; new_test_ext().execute_with(|| { - assert!(>::iter_values().count() > 0); - assert!(>::exists()); + assert!(HistoricalSessions::::iter_values().count() > 0); + assert!(StoredRange::::exists()); let old_pallet = "Session"; let new_pallet = ::name();