Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
remove every usage of #[pallet::getter]
Browse files Browse the repository at this point in the history
Signed-off-by: muraca <mmuraca247@gmail.com>
  • Loading branch information
muraca committed Mar 29, 2023
1 parent 1d8208b commit 271f6a2
Show file tree
Hide file tree
Showing 19 changed files with 73 additions and 64 deletions.
14 changes: 7 additions & 7 deletions pallets/collator-selection/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,8 @@ pub mod pallet {

// ensure we are below limit.
let length = <Candidates<T>>::decode_len().unwrap_or_default();
ensure!((length as u32) < Self::desired_candidates(), Error::<T>::TooManyCandidates);
ensure!(!Self::invulnerables().contains(&who), Error::<T>::AlreadyInvulnerable);
ensure!((length as u32) < DesiredCandidates::<T>::get(), Error::<T>::TooManyCandidates);
ensure!(!Invulnerables::<T>::get().contains(&who), Error::<T>::AlreadyInvulnerable);

let validator_key = T::ValidatorIdOf::convert(who.clone())
.ok_or(Error::<T>::NoAssociatedValidatorId)?;
Expand All @@ -365,7 +365,7 @@ pub mod pallet {
Error::<T>::ValidatorNotRegistered
);

let deposit = Self::candidacy_bond();
let deposit = CandidacyBond::<T>::get();
// First authored block is current block plus kick threshold to handle session delay
let incoming = CandidateInfo { who: who.clone(), deposit };

Expand Down Expand Up @@ -399,7 +399,7 @@ pub mod pallet {
pub fn leave_intent(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;
ensure!(
Self::candidates().len() as u32 > T::MinCandidates::get(),
Candidates::<T>::get().len() as u32 > T::MinCandidates::get(),
Error::<T>::TooFewCandidates
);
let current_count = Self::try_remove_candidate(&who)?;
Expand Down Expand Up @@ -437,7 +437,7 @@ pub mod pallet {
pub fn assemble_collators(
candidates: BoundedVec<T::AccountId, T::MaxCandidates>,
) -> Vec<T::AccountId> {
let mut collators = Self::invulnerables().to_vec();
let mut collators = Invulnerables::<T>::get().to_vec();
collators.extend(candidates);
collators
}
Expand All @@ -455,7 +455,7 @@ pub mod pallet {
let last_block = <LastAuthoredBlock<T>>::get(c.who.clone());
let since_last = now.saturating_sub(last_block);
if since_last < kick_threshold ||
Self::candidates().len() as u32 <= T::MinCandidates::get()
Candidates::<T>::get().len() as u32 <= T::MinCandidates::get()
{
Some(c.who)
} else {
Expand Down Expand Up @@ -506,7 +506,7 @@ pub mod pallet {
<frame_system::Pallet<T>>::block_number(),
);

let candidates = Self::candidates();
let candidates = Candidates::<T>::get();
let candidates_len_before = candidates.len();
let active_candidates = Self::kick_stale_candidates(candidates);
let removed = candidates_len_before - active_candidates.len();
Expand Down
58 changes: 30 additions & 28 deletions pallets/collator-selection/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
// limitations under the License.

use crate as collator_selection;
use crate::{mock::*, CandidateInfo, Error};
use crate::{
mock::*, CandidacyBond, CandidateInfo, Candidates, DesiredCandidates, Error, Invulnerables,
};
use frame_support::{
assert_noop, assert_ok,
traits::{Currency, GenesisBuild, OnInitialize},
Expand All @@ -25,11 +27,11 @@ use sp_runtime::traits::BadOrigin;
#[test]
fn basic_setup_works() {
new_test_ext().execute_with(|| {
assert_eq!(CollatorSelection::desired_candidates(), 2);
assert_eq!(CollatorSelection::candidacy_bond(), 10);
assert_eq!(DesiredCandidates::<Test>::get(), 2);
assert_eq!(CandidacyBond::<Test>::get(), 10);

assert!(CollatorSelection::candidates().is_empty());
assert_eq!(CollatorSelection::invulnerables(), vec![1, 2]);
assert!(Candidates::<Test>::get().is_empty());
assert_eq!(Invulnerables::<Test>::get(), vec![1, 2]);
});
}

Expand All @@ -41,7 +43,7 @@ fn it_should_set_invulnerables() {
RuntimeOrigin::signed(RootAccount::get()),
new_set.clone()
));
assert_eq!(CollatorSelection::invulnerables(), new_set);
assert_eq!(Invulnerables::<Test>::get(), new_set);

// cannot set with non-root.
assert_noop!(
Expand All @@ -65,14 +67,14 @@ fn it_should_set_invulnerables() {
fn set_desired_candidates_works() {
new_test_ext().execute_with(|| {
// given
assert_eq!(CollatorSelection::desired_candidates(), 2);
assert_eq!(DesiredCandidates::<Test>::get(), 2);

// can set
assert_ok!(CollatorSelection::set_desired_candidates(
RuntimeOrigin::signed(RootAccount::get()),
7
));
assert_eq!(CollatorSelection::desired_candidates(), 7);
assert_eq!(DesiredCandidates::<Test>::get(), 7);

// rejects bad origin
assert_noop!(
Expand All @@ -86,14 +88,14 @@ fn set_desired_candidates_works() {
fn set_candidacy_bond() {
new_test_ext().execute_with(|| {
// given
assert_eq!(CollatorSelection::candidacy_bond(), 10);
assert_eq!(CandidacyBond::<Test>::get(), 10);

// can set
assert_ok!(CollatorSelection::set_candidacy_bond(
RuntimeOrigin::signed(RootAccount::get()),
7
));
assert_eq!(CollatorSelection::candidacy_bond(), 7);
assert_eq!(CandidacyBond::<Test>::get(), 7);

// rejects bad origin.
assert_noop!(CollatorSelection::set_candidacy_bond(RuntimeOrigin::signed(1), 8), BadOrigin);
Expand Down Expand Up @@ -142,7 +144,7 @@ fn cannot_unregister_candidate_if_too_few() {
#[test]
fn cannot_register_as_candidate_if_invulnerable() {
new_test_ext().execute_with(|| {
assert_eq!(CollatorSelection::invulnerables(), vec![1, 2]);
assert_eq!(Invulnerables::<Test>::get(), vec![1, 2]);

// can't 1 because it is invulnerable.
assert_noop!(
Expand All @@ -169,7 +171,7 @@ fn cannot_register_dupe_candidate() {
// can add 3 as candidate
assert_ok!(CollatorSelection::register_as_candidate(RuntimeOrigin::signed(3)));
let addition = CandidateInfo { who: 3, deposit: 10 };
assert_eq!(CollatorSelection::candidates(), vec![addition]);
assert_eq!(Candidates::<Test>::get(), vec![addition]);
assert_eq!(CollatorSelection::last_authored_block(3), 10);
assert_eq!(Balances::free_balance(3), 90);

Expand Down Expand Up @@ -202,10 +204,10 @@ fn cannot_register_as_candidate_if_poor() {
fn register_as_candidate_works() {
new_test_ext().execute_with(|| {
// given
assert_eq!(CollatorSelection::desired_candidates(), 2);
assert_eq!(CollatorSelection::candidacy_bond(), 10);
assert_eq!(CollatorSelection::candidates(), Vec::new());
assert_eq!(CollatorSelection::invulnerables(), vec![1, 2]);
assert_eq!(DesiredCandidates::<Test>::get(), 2);
assert_eq!(CandidacyBond::<Test>::get(), 10);
assert_eq!(Candidates::<Test>::get(), Vec::new());
assert_eq!(Invulnerables::<Test>::get(), vec![1, 2]);

// take two endowed, non-invulnerables accounts.
assert_eq!(Balances::free_balance(&3), 100);
Expand All @@ -217,7 +219,7 @@ fn register_as_candidate_works() {
assert_eq!(Balances::free_balance(&3), 90);
assert_eq!(Balances::free_balance(&4), 90);

assert_eq!(CollatorSelection::candidates().len(), 2);
assert_eq!(Candidates::<Test>::get().len(), 2);
});
}

Expand Down Expand Up @@ -259,7 +261,7 @@ fn authorship_event_handler() {

let collator = CandidateInfo { who: 4, deposit: 10 };

assert_eq!(CollatorSelection::candidates(), vec![collator]);
assert_eq!(Candidates::<Test>::get(), vec![collator]);
assert_eq!(CollatorSelection::last_authored_block(4), 0);

// half of the pot goes to the collator who's the author (4 in tests).
Expand All @@ -284,7 +286,7 @@ fn fees_edgecases() {

let collator = CandidateInfo { who: 4, deposit: 10 };

assert_eq!(CollatorSelection::candidates(), vec![collator]);
assert_eq!(Candidates::<Test>::get(), vec![collator]);
assert_eq!(CollatorSelection::last_authored_block(4), 0);
// Nothing received
assert_eq!(Balances::free_balance(4), 90);
Expand Down Expand Up @@ -312,14 +314,14 @@ fn session_management_works() {
// session won't see this.
assert_eq!(SessionHandlerCollators::get(), vec![1, 2]);
// but we have a new candidate.
assert_eq!(CollatorSelection::candidates().len(), 1);
assert_eq!(Candidates::<Test>::get().len(), 1);

initialize_to_block(10);
assert_eq!(SessionChangeBlock::get(), 10);
// pallet-session has 1 session delay; current validators are the same.
assert_eq!(Session::validators(), vec![1, 2]);
assert_eq!(pallet_session::Validators::<Test>::get(), vec![1, 2]);
// queued ones are changed, and now we have 3.
assert_eq!(Session::queued_keys().len(), 3);
assert_eq!(pallet_session::QueuedKeys::<Test>::get().len(), 3);
// session handlers (aura, et. al.) cannot see this yet.
assert_eq!(SessionHandlerCollators::get(), vec![1, 2]);

Expand All @@ -337,15 +339,15 @@ fn kick_mechanism() {
assert_ok!(CollatorSelection::register_as_candidate(RuntimeOrigin::signed(3)));
assert_ok!(CollatorSelection::register_as_candidate(RuntimeOrigin::signed(4)));
initialize_to_block(10);
assert_eq!(CollatorSelection::candidates().len(), 2);
assert_eq!(Candidates::<Test>::get().len(), 2);
initialize_to_block(20);
assert_eq!(SessionChangeBlock::get(), 20);
// 4 authored this block, gets to stay 3 was kicked
assert_eq!(CollatorSelection::candidates().len(), 1);
assert_eq!(Candidates::<Test>::get().len(), 1);
// 3 will be kicked after 1 session delay
assert_eq!(SessionHandlerCollators::get(), vec![1, 2, 3, 4]);
let collator = CandidateInfo { who: 4, deposit: 10 };
assert_eq!(CollatorSelection::candidates(), vec![collator]);
assert_eq!(Candidates::<Test>::get(), vec![collator]);
assert_eq!(CollatorSelection::last_authored_block(4), 20);
initialize_to_block(30);
// 3 gets kicked after 1 session delay
Expand All @@ -362,15 +364,15 @@ fn should_not_kick_mechanism_too_few() {
assert_ok!(CollatorSelection::register_as_candidate(RuntimeOrigin::signed(3)));
assert_ok!(CollatorSelection::register_as_candidate(RuntimeOrigin::signed(5)));
initialize_to_block(10);
assert_eq!(CollatorSelection::candidates().len(), 2);
assert_eq!(Candidates::<Test>::get().len(), 2);
initialize_to_block(20);
assert_eq!(SessionChangeBlock::get(), 20);
// 4 authored this block, 5 gets to stay too few 3 was kicked
assert_eq!(CollatorSelection::candidates().len(), 1);
assert_eq!(Candidates::<Test>::get().len(), 1);
// 3 will be kicked after 1 session delay
assert_eq!(SessionHandlerCollators::get(), vec![1, 2, 3, 5]);
let collator = CandidateInfo { who: 5, deposit: 10 };
assert_eq!(CollatorSelection::candidates(), vec![collator]);
assert_eq!(Candidates::<Test>::get(), vec![collator]);
assert_eq!(CollatorSelection::last_authored_block(4), 20);
initialize_to_block(30);
// 3 gets kicked after 1 session delay
Expand Down
20 changes: 9 additions & 11 deletions pallets/parachain-system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ pub mod pallet {
"set_validation_data inherent needs to be present in every block!"
);

let host_config = match Self::host_configuration() {
let host_config = match HostConfiguration::<T>::get() {
Some(ok) => ok,
None => {
debug_assert!(
Expand All @@ -220,7 +220,7 @@ pub mod pallet {
return
},
};
let relevant_messaging_state = match Self::relevant_messaging_state() {
let relevant_messaging_state = match RelevantMessagingState::<T>::get() {
Some(ok) => ok,
None => {
debug_assert!(
Expand Down Expand Up @@ -325,7 +325,7 @@ pub mod pallet {
// than the announced, we would waste some of weight. In the case the actual value is
// greater than the announced, we will miss opportunity to send a couple of messages.
weight += T::DbWeight::get().reads_writes(1, 1);
let hrmp_max_message_num_per_candidate = Self::host_configuration()
let hrmp_max_message_num_per_candidate = HostConfiguration::<T>::get()
.map(|cfg| cfg.hrmp_max_message_num_per_candidate)
.unwrap_or(0);
<AnnouncedHrmpMessagesPerCandidate<T>>::put(hrmp_max_message_num_per_candidate);
Expand Down Expand Up @@ -781,7 +781,7 @@ impl<T: Config> GetChannelInfo for Pallet<T> {
//
// Here it a similar case, with the difference that the realization that the channel is
// closed came the same block.
let channels = match Self::relevant_messaging_state() {
let channels = match RelevantMessagingState::<T>::get() {
None => {
log::warn!("calling `get_channel_status` with no RelevantMessagingState?!");
return ChannelStatus::Closed
Expand Down Expand Up @@ -809,7 +809,7 @@ impl<T: Config> GetChannelInfo for Pallet<T> {
}

fn get_channel_max(id: ParaId) -> Option<usize> {
let channels = Self::relevant_messaging_state()?.egress_channels;
let channels = RelevantMessagingState::<T>::get()?.egress_channels;
let index = channels.binary_search_by_key(&id, |item| item.0).ok()?;
Some(channels[index].1.max_message_size as usize)
}
Expand Down Expand Up @@ -989,7 +989,7 @@ impl<T: Config> Pallet<T> {
ensure!(<UpgradeRestrictionSignal<T>>::get().is_none(), Error::<T>::ProhibitedByPolkadot);

ensure!(!<PendingValidationCode<T>>::exists(), Error::<T>::OverlappingUpgrades);
let cfg = Self::host_configuration().ok_or(Error::<T>::HostConfigurationNotAvailable)?;
let cfg = HostConfiguration::<T>::get().ok_or(Error::<T>::HostConfigurationNotAvailable)?;
ensure!(validation_function.len() <= cfg.max_code_size as usize, Error::<T>::TooBig);

// When a code upgrade is scheduled, it has to be applied in two
Expand Down Expand Up @@ -1066,7 +1066,7 @@ impl<T: Config> Pallet<T> {
// may change so that the message is no longer valid.
//
// However, changing this setting is expected to be rare.
match Self::host_configuration() {
match HostConfiguration::<T>::get() {
Some(cfg) =>
if message.len() > cfg.max_upward_message_size as usize {
return Err(MessageSendError::TooBig)
Expand Down Expand Up @@ -1157,9 +1157,7 @@ impl<T: Config> BlockNumberProvider for RelaychainBlockNumberProvider<T> {
type BlockNumber = relay_chain::BlockNumber;

fn current_block_number() -> relay_chain::BlockNumber {
Pallet::<T>::validation_data()
.map(|d| d.relay_parent_number)
.unwrap_or_default()
ValidationData::<T>::get().map(|d| d.relay_parent_number).unwrap_or_default()
}

#[cfg(feature = "runtime-benchmarks")]
Expand Down Expand Up @@ -1204,7 +1202,7 @@ impl<T: Config> BlockNumberProvider for RelaychainDataProvider<T> {

#[cfg(feature = "runtime-benchmarks")]
fn set_block_number(block: Self::BlockNumber) {
let mut validation_data = Pallet::<T>::validation_data().unwrap_or_else(||
let mut validation_data = ValidationData::<T>::get().unwrap_or_else(||
// PersistedValidationData does not impl default in non-std
PersistedValidationData {
parent_head: vec![].into(),
Expand Down
1 change: 0 additions & 1 deletion parachain-template/pallets/template/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ pub mod pallet {
// The pallet's runtime storage items.
// https://docs.substrate.io/v3/runtime/storage
#[pallet::storage]
#[pallet::getter(fn something)]
// Learn more about declaring storage items:
// https://docs.substrate.io/v3/runtime/storage#declaring-storage-items
pub type Something<T> = StorageValue<_, u32>;
Expand Down
4 changes: 2 additions & 2 deletions parachain-template/pallets/template/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{mock::*, Error};
use crate::{mock::*, Error, Something};
use frame_support::{assert_noop, assert_ok};

#[test]
Expand All @@ -7,7 +7,7 @@ fn it_works_for_default_value() {
// Dispatch a signed extrinsic.
assert_ok!(TemplateModule::do_something(RuntimeOrigin::signed(1), 42));
// Read pallet storage and assert an expected result.
assert_eq!(TemplateModule::something(), Some(42));
assert_eq!(Something::<Test>::get(), Some(42));
});
}

Expand Down
3 changes: 2 additions & 1 deletion parachain-template/runtime/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use frame_system::EnsureRoot;
use pallet_xcm::XcmPassthrough;
use polkadot_parachain::primitives::Sibling;
use polkadot_runtime_common::impls::ToAuthor;
use sp_core::Get;
use xcm::{latest::prelude::*, CreateMatcher, MatchXcm};
use xcm_builder::{
AccountId32Aliases, AllowExplicitUnpaidExecutionFrom, AllowTopLevelPaidExecutionFrom,
Expand All @@ -26,7 +27,7 @@ parameter_types! {
pub const RelayLocation: MultiLocation = MultiLocation::parent();
pub const RelayNetwork: Option<NetworkId> = None;
pub RelayChainOrigin: RuntimeOrigin = cumulus_pallet_xcm::Origin::Relay.into();
pub UniversalLocation: InteriorMultiLocation = Parachain(ParachainInfo::parachain_id().into()).into();
pub UniversalLocation: InteriorMultiLocation = Parachain(ParachainInfo::get().into()).into();
}

/// Type for specifying how a `MultiLocation` can be converted into an `AccountId`. This is used
Expand Down
2 changes: 1 addition & 1 deletion parachains/pallets/parachain-info/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub mod pallet {

impl<T: Config> Get<ParaId> for Pallet<T> {
fn get() -> ParaId {
Self::parachain_id()
ParachainId::<T>::get()
}
}
}
3 changes: 2 additions & 1 deletion parachains/runtimes/assets/statemine/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use parachains_common::{
},
};
use polkadot_parachain::primitives::Sibling;
use sp_core::Get;
use sp_runtime::traits::ConvertInto;
use xcm::latest::prelude::*;
use xcm_builder::{
Expand All @@ -51,7 +52,7 @@ parameter_types! {
pub const RelayNetwork: Option<NetworkId> = Some(NetworkId::Kusama);
pub RelayChainOrigin: RuntimeOrigin = cumulus_pallet_xcm::Origin::Relay.into();
pub UniversalLocation: InteriorMultiLocation =
X2(GlobalConsensus(RelayNetwork::get().unwrap()), Parachain(ParachainInfo::parachain_id().into()));
X2(GlobalConsensus(RelayNetwork::get().unwrap()), Parachain(ParachainInfo::get().into()));
pub UniversalLocationNetworkId: NetworkId = UniversalLocation::get().global_consensus().unwrap();
pub TrustBackedAssetsPalletLocation: MultiLocation =
PalletInstance(<Assets as PalletInfoAccess>::index() as u8).into();
Expand Down
Loading

0 comments on commit 271f6a2

Please sign in to comment.