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

Thread-local parameter_types for testing. #7542

Merged
10 commits merged into from
Nov 20, 2020
11 changes: 2 additions & 9 deletions frame/balances/src/tests_composite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ use sp_runtime::{
use sp_core::H256;
use sp_io;
use frame_support::{impl_outer_origin, impl_outer_event, parameter_types};
use frame_support::traits::Get;
use frame_support::weights::{Weight, DispatchInfo, IdentityFee};
use pallet_transaction_payment::CurrencyAdapter;
use std::cell::RefCell;
use crate::{GenesisConfig, Module, Trait, decl_tests, tests::CallWithDispatchInfo};

use frame_system as system;
Expand All @@ -49,13 +47,8 @@ impl_outer_event! {
}
}

thread_local! {
static EXISTENTIAL_DEPOSIT: RefCell<u64> = RefCell::new(0);
}

pub struct ExistentialDeposit;
impl Get<u64> for ExistentialDeposit {
fn get() -> u64 { EXISTENTIAL_DEPOSIT.with(|v| *v.borrow()) }
frame_support::parameter_types_thread_local! {
static ExistentialDeposit: u64 = 0;
}

// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
Expand Down
12 changes: 3 additions & 9 deletions frame/balances/src/tests_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ use sp_runtime::{
use sp_core::H256;
use sp_io;
use frame_support::{impl_outer_origin, impl_outer_event, parameter_types};
use frame_support::traits::{Get, StorageMapShim};
use frame_support::traits::StorageMapShim;
use frame_support::weights::{Weight, DispatchInfo, IdentityFee};
use std::cell::RefCell;
use crate::{GenesisConfig, Module, Trait, decl_tests, tests::CallWithDispatchInfo};
use pallet_transaction_payment::CurrencyAdapter;

Expand All @@ -49,13 +48,8 @@ impl_outer_event! {
}
}

thread_local! {
static EXISTENTIAL_DEPOSIT: RefCell<u64> = RefCell::new(0);
}

pub struct ExistentialDeposit;
impl Get<u64> for ExistentialDeposit {
fn get() -> u64 { EXISTENTIAL_DEPOSIT.with(|v| *v.borrow()) }
frame_support::parameter_types_thread_local! {
static ExistentialDeposit: u64 = 0;
}

// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
Expand Down
12 changes: 3 additions & 9 deletions frame/contracts/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ use sp_runtime::{
use frame_support::{
assert_ok, assert_err_ignore_postinfo, impl_outer_dispatch, impl_outer_event,
impl_outer_origin, parameter_types, StorageMap, StorageValue,
traits::{Currency, Get, ReservableCurrency},
traits::{Currency, ReservableCurrency},
weights::{Weight, PostDispatchInfo},
dispatch::DispatchErrorWithPostInfo,
};
use std::cell::RefCell;
use frame_system::{self as system, EventRecord, Phase};

mod contracts {
Expand Down Expand Up @@ -99,13 +98,8 @@ pub mod test_utils {
}
}

thread_local! {
static EXISTENTIAL_DEPOSIT: RefCell<u64> = RefCell::new(0);
}

pub struct ExistentialDeposit;
impl Get<u64> for ExistentialDeposit {
fn get() -> u64 { EXISTENTIAL_DEPOSIT.with(|v| *v.borrow()) }
frame_support::parameter_types_thread_local! {
static ExistentialDeposit: u64 = 0;
}

#[derive(Clone, Eq, PartialEq, Debug)]
Expand Down
17 changes: 5 additions & 12 deletions frame/democracy/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
//! The crate's tests.

use super::*;
use std::cell::RefCell;
use codec::Encode;
use frame_support::{
impl_outer_origin, impl_outer_dispatch, assert_noop, assert_ok, parameter_types,
Expand Down Expand Up @@ -171,18 +170,12 @@ impl Contains<u64> for OneToFive {
#[cfg(feature = "runtime-benchmarks")]
fn add(_m: &u64) {}
}
thread_local! {
static PREIMAGE_BYTE_DEPOSIT: RefCell<u64> = RefCell::new(0);
static INSTANT_ALLOWED: RefCell<bool> = RefCell::new(false);
}
pub struct PreimageByteDeposit;
impl Get<u64> for PreimageByteDeposit {
fn get() -> u64 { PREIMAGE_BYTE_DEPOSIT.with(|v| *v.borrow()) }
}
pub struct InstantAllowed;
impl Get<bool> for InstantAllowed {
fn get() -> bool { INSTANT_ALLOWED.with(|v| *v.borrow()) }

frame_support::parameter_types_thread_local! {
static PreimageByteDeposit: u64 = 0;
static InstantAllowed: bool = false;
}

impl super::Trait for Test {
type Proposal = Call;
type Event = Event;
Expand Down
38 changes: 7 additions & 31 deletions frame/elections-phragmen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,6 @@ impl<T: Trait> ContainsLengthBound for Module<T> {
#[cfg(test)]
mod tests {
use super::*;
use std::cell::RefCell;
use frame_support::{assert_ok, assert_noop, assert_err_with_weight, parameter_types,
weights::Weight,
};
Expand Down Expand Up @@ -1117,36 +1116,13 @@ mod tests {
pub const CandidacyBond: u64 = 3;
}

thread_local! {
static VOTING_BOND: RefCell<u64> = RefCell::new(2);
static DESIRED_MEMBERS: RefCell<u32> = RefCell::new(2);
static DESIRED_RUNNERS_UP: RefCell<u32> = RefCell::new(2);
static TERM_DURATION: RefCell<u64> = RefCell::new(5);
}

pub struct VotingBond;
impl Get<u64> for VotingBond {
fn get() -> u64 { VOTING_BOND.with(|v| *v.borrow()) }
}

pub struct DesiredMembers;
impl Get<u32> for DesiredMembers {
fn get() -> u32 { DESIRED_MEMBERS.with(|v| *v.borrow()) }
}

pub struct DesiredRunnersUp;
impl Get<u32> for DesiredRunnersUp {
fn get() -> u32 { DESIRED_RUNNERS_UP.with(|v| *v.borrow()) }
}

pub struct TermDuration;
impl Get<u64> for TermDuration {
fn get() -> u64 { TERM_DURATION.with(|v| *v.borrow()) }
}

thread_local! {
pub static MEMBERS: RefCell<Vec<u64>> = RefCell::new(vec![]);
pub static PRIME: RefCell<Option<u64>> = RefCell::new(None);
frame_support::parameter_types_thread_local! {
static VotingBond: u64 = 2;
static DesiredMembers: u32 = 2;
static DesiredRunnersUp: u32 = 2;
static TermDuration: u64 = 5;
static Members: Vec<u64> = vec![];
static Prime: Option<u64> = None;
}

pub struct TestChangeMembers;
Expand Down
37 changes: 8 additions & 29 deletions frame/elections/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@

#![cfg(test)]

use std::cell::RefCell;
use frame_support::{
StorageValue, StorageMap, parameter_types, assert_ok,
traits::{Get, ChangeMembers, Currency, LockIdentifier},
traits::{ChangeMembers, Currency, LockIdentifier},
weights::Weight,
};
use sp_core::H256;
Expand Down Expand Up @@ -87,32 +86,12 @@ parameter_types! {
pub const MinimumVotingLock: u64 = 5;
}

thread_local! {
static VOTER_BOND: RefCell<u64> = RefCell::new(0);
static VOTING_FEE: RefCell<u64> = RefCell::new(0);
static PRESENT_SLASH_PER_VOTER: RefCell<u64> = RefCell::new(0);
static DECAY_RATIO: RefCell<u32> = RefCell::new(0);
static MEMBERS: RefCell<Vec<u64>> = RefCell::new(vec![]);
}

pub struct VotingBond;
impl Get<u64> for VotingBond {
fn get() -> u64 { VOTER_BOND.with(|v| *v.borrow()) }
}

pub struct VotingFee;
impl Get<u64> for VotingFee {
fn get() -> u64 { VOTING_FEE.with(|v| *v.borrow()) }
}

pub struct PresentSlashPerVoter;
impl Get<u64> for PresentSlashPerVoter {
fn get() -> u64 { PRESENT_SLASH_PER_VOTER.with(|v| *v.borrow()) }
}

pub struct DecayRatio;
impl Get<u32> for DecayRatio {
fn get() -> u32 { DECAY_RATIO.with(|v| *v.borrow()) }
frame_support::parameter_types_thread_local! {
static VoterBond: u64 = 0;
kianenigma marked this conversation as resolved.
Show resolved Hide resolved
static VotingFee: u64 = 0;
static PresentSlashPerVoter: u64 = 0;
static DecayRatio: u32 = 0;
static Members: Vec<u64> = vec![];
}

pub struct TestChangeMembers;
Expand Down Expand Up @@ -143,7 +122,7 @@ impl elections::Trait for Test {
type LoserCandidate = ();
type ChangeMembers = TestChangeMembers;
type CandidacyBond = CandidacyBond;
type VotingBond = VotingBond;
type VotingBond = VoterBond;
kianenigma marked this conversation as resolved.
Show resolved Hide resolved
type VotingFee = VotingFee;
type MinimumVotingLock = MinimumVotingLock;
type PresentSlashPerVoter = PresentSlashPerVoter;
Expand Down
7 changes: 3 additions & 4 deletions frame/membership/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,6 @@ impl<T: Trait<I>, I: Instance> Contains<T::AccountId> for Module<T, I> {
mod tests {
use super::*;

use std::cell::RefCell;
use frame_support::{
assert_ok, assert_noop, impl_outer_origin, parameter_types, weights::Weight,
ord_parameter_types
Expand Down Expand Up @@ -334,9 +333,9 @@ mod tests {
pub const Five: u64 = 5;
}

thread_local! {
static MEMBERS: RefCell<Vec<u64>> = RefCell::new(vec![]);
static PRIME: RefCell<Option<u64>> = RefCell::new(None);
frame_support::parameter_types_thread_local! {
static Members: Vec<u64> = vec![];
static Prime: Option<u64> = None;
}

pub struct TestChangeMembers;
Expand Down
5 changes: 2 additions & 3 deletions frame/scored-pool/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

use super::*;

use std::cell::RefCell;
use frame_support::{impl_outer_origin, parameter_types, weights::Weight, ord_parameter_types};
use sp_core::H256;
use sp_runtime::{
Expand Down Expand Up @@ -87,8 +86,8 @@ impl pallet_balances::Trait for Test {
type WeightInfo = ();
}

thread_local! {
pub static MEMBERS: RefCell<Vec<u64>> = RefCell::new(vec![]);
frame_support::parameter_types_thread_local! {
static Members: Vec<u64> = vec![];
}

pub struct TestChangeMembers;
Expand Down
65 changes: 11 additions & 54 deletions frame/staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,6 @@ pub(crate) type Balance = u128;

thread_local! {
static SESSION: RefCell<(Vec<AccountId>, HashSet<AccountId>)> = RefCell::new(Default::default());
static SESSION_PER_ERA: RefCell<SessionIndex> = RefCell::new(3);
static EXISTENTIAL_DEPOSIT: RefCell<Balance> = RefCell::new(0);
static SLASH_DEFER_DURATION: RefCell<EraIndex> = RefCell::new(0);
static ELECTION_LOOKAHEAD: RefCell<BlockNumber> = RefCell::new(0);
static PERIOD: RefCell<BlockNumber> = RefCell::new(1);
static MAX_ITERATIONS: RefCell<u32> = RefCell::new(0);
}

/// Another session handler struct to test on_disabled.
Expand Down Expand Up @@ -92,53 +86,6 @@ pub fn is_disabled(controller: AccountId) -> bool {
SESSION.with(|d| d.borrow().1.contains(&stash))
}

pub struct ExistentialDeposit;
impl Get<Balance> for ExistentialDeposit {
fn get() -> Balance {
EXISTENTIAL_DEPOSIT.with(|v| *v.borrow())
}
}

pub struct SessionsPerEra;
impl Get<SessionIndex> for SessionsPerEra {
fn get() -> SessionIndex {
SESSION_PER_ERA.with(|v| *v.borrow())
}
}
impl Get<BlockNumber> for SessionsPerEra {
fn get() -> BlockNumber {
SESSION_PER_ERA.with(|v| *v.borrow() as BlockNumber)
}
}

pub struct ElectionLookahead;
impl Get<BlockNumber> for ElectionLookahead {
fn get() -> BlockNumber {
ELECTION_LOOKAHEAD.with(|v| *v.borrow())
}
}

pub struct Period;
impl Get<BlockNumber> for Period {
fn get() -> BlockNumber {
PERIOD.with(|v| *v.borrow())
}
}

pub struct SlashDeferDuration;
impl Get<EraIndex> for SlashDeferDuration {
fn get() -> EraIndex {
SLASH_DEFER_DURATION.with(|v| *v.borrow())
}
}

pub struct MaxIterations;
impl Get<u32> for MaxIterations {
fn get() -> u32 {
MAX_ITERATIONS.with(|v| *v.borrow())
}
}

impl_outer_origin! {
pub enum Origin for Test where system = frame_system {}
}
Expand Down Expand Up @@ -187,6 +134,16 @@ parameter_types! {
pub const AvailableBlockRatio: Perbill = Perbill::one();
pub const MaxLocks: u32 = 1024;
}

frame_support::parameter_types_thread_local! {
static SessionsPerEra: SessionIndex = 3;
static ExistentialDeposit: Balance = 0;
static SlashDeferDuration: EraIndex = 0;
static ElectionLookahead: BlockNumber = 0;
static Period: BlockNumber = 1;
static MaxIterations: u32 = 0;
}

impl frame_system::Trait for Test {
type BaseCallFilter = ();
type Origin = Origin;
Expand Down Expand Up @@ -437,7 +394,7 @@ impl ExtBuilder {
pub fn set_associated_constants(&self) {
EXISTENTIAL_DEPOSIT.with(|v| *v.borrow_mut() = self.existential_deposit);
SLASH_DEFER_DURATION.with(|v| *v.borrow_mut() = self.slash_defer_duration);
SESSION_PER_ERA.with(|v| *v.borrow_mut() = self.session_per_era);
SESSIONS_PER_ERA.with(|v| *v.borrow_mut() = self.session_per_era);
ELECTION_LOOKAHEAD.with(|v| *v.borrow_mut() = self.election_lookahead);
PERIOD.with(|v| *v.borrow_mut() = self.session_length);
MAX_ITERATIONS.with(|v| *v.borrow_mut() = self.max_offchain_iterations);
Expand Down
Loading