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
12 changes: 1 addition & 11 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,15 +47,6 @@ 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()) }
}

// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Test;
Expand All @@ -66,6 +55,7 @@ parameter_types! {
pub const MaximumBlockWeight: Weight = 1024;
pub const MaximumBlockLength: u32 = 2 * 1024;
pub const AvailableBlockRatio: Perbill = Perbill::one();
pub static ExistentialDeposit: u64 = 0;
}
impl frame_system::Trait for Test {
type BaseCallFilter = ();
Expand Down
13 changes: 2 additions & 11 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,15 +48,6 @@ 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()) }
}

// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Test;
Expand All @@ -66,6 +56,7 @@ parameter_types! {
pub const MaximumBlockWeight: Weight = 1024;
pub const MaximumBlockLength: u32 = 2 * 1024;
pub const AvailableBlockRatio: Perbill = Perbill::one();
pub static ExistentialDeposit: u64 = 0;
}
impl frame_system::Trait for Test {
type BaseCallFilter = ();
Expand Down
13 changes: 2 additions & 11 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,22 +98,14 @@ 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()) }
}

#[derive(Clone, Eq, PartialEq, Debug)]
pub struct Test;
parameter_types! {
pub const BlockHashCount: u64 = 250;
pub const MaximumBlockWeight: Weight = 1024;
pub const MaximumBlockLength: u32 = 2 * 1024;
pub const AvailableBlockRatio: Perbill = Perbill::one();
pub static ExistentialDeposit: u64 = 0;
}
impl frame_system::Trait for Test {
type BaseCallFilter = ();
Expand Down
16 changes: 3 additions & 13 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 @@ -154,6 +153,8 @@ parameter_types! {
pub const CooloffPeriod: u64 = 2;
pub const MaxVotes: u32 = 100;
pub const MaxProposals: u32 = MAX_PROPOSALS;
pub static PreimageByteDeposit: u64 = 0;
pub static InstantAllowed: bool = false;
}
ord_parameter_types! {
pub const One: u64 = 1;
Expand All @@ -171,18 +172,7 @@ 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()) }
}

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! {
pub static VotingBond: u64 = 2;
pub static DesiredMembers: u32 = 2;
pub static DesiredRunnersUp: u32 = 2;
pub static TermDuration: u64 = 5;
pub static Members: Vec<u64> = vec![];
pub static Prime: Option<u64> = None;
}

pub struct TestChangeMembers;
Expand Down
46 changes: 11 additions & 35 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 @@ -85,34 +84,11 @@ parameter_types! {
pub const InactiveGracePeriod: u32 = 1;
pub const VotingPeriod: u64 = 4;
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()) }
pub static VotingBond: u64 = 0;
pub static VotingFee: u64 = 0;
pub static PresentSlashPerVoter: u64 = 0;
pub static DecayRatio: u32 = 0;
pub static Members: Vec<u64> = vec![];
}

pub struct TestChangeMembers;
Expand Down Expand Up @@ -175,7 +151,7 @@ pub struct ExtBuilder {
decay_ratio: u32,
desired_seats: u32,
voting_fee: u64,
voter_bond: u64,
voting_bond: u64,
bad_presentation_punishment: u64,
}

Expand All @@ -186,7 +162,7 @@ impl Default for ExtBuilder {
decay_ratio: 24,
desired_seats: 2,
voting_fee: 0,
voter_bond: 0,
voting_bond: 0,
bad_presentation_punishment: 1,
}
}
Expand All @@ -209,16 +185,16 @@ impl ExtBuilder {
self.bad_presentation_punishment = fee;
self
}
pub fn voter_bond(mut self, fee: u64) -> Self {
self.voter_bond = fee;
pub fn voting_bond(mut self, fee: u64) -> Self {
self.voting_bond = fee;
self
}
pub fn desired_seats(mut self, seats: u32) -> Self {
self.desired_seats = seats;
self
}
pub fn build(self) -> sp_io::TestExternalities {
VOTER_BOND.with(|v| *v.borrow_mut() = self.voter_bond);
VOTING_BOND.with(|v| *v.borrow_mut() = self.voting_bond);
VOTING_FEE.with(|v| *v.borrow_mut() = self.voting_fee);
PRESENT_SLASH_PER_VOTER.with(|v| *v.borrow_mut() = self.bad_presentation_punishment);
DECAY_RATIO.with(|v| *v.borrow_mut() = self.decay_ratio);
Expand Down
12 changes: 6 additions & 6 deletions frame/elections/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ fn voting_initial_set_approvals_ignores_voter_index() {
}
#[test]
fn voting_bad_approval_index_slashes_voters_and_bond_reduces_stake() {
ExtBuilder::default().voting_fee(5).voter_bond(2).build().execute_with(|| {
ExtBuilder::default().voting_fee(5).voting_bond(2).build().execute_with(|| {
assert_ok!(Elections::submit_candidacy(Origin::signed(2), 0));

(1..=63).for_each(|i| vote(i, 0));
Expand Down Expand Up @@ -365,7 +365,7 @@ fn voting_cannot_lock_less_than_limit() {

#[test]
fn voting_locking_more_than_total_balance_is_moot() {
ExtBuilder::default().voter_bond(2).build().execute_with(|| {
ExtBuilder::default().voting_bond(2).build().execute_with(|| {
assert_ok!(Elections::submit_candidacy(Origin::signed(2), 0));

assert_eq!(balances(&3), (30, 0));
Expand All @@ -381,7 +381,7 @@ fn voting_locking_more_than_total_balance_is_moot() {

#[test]
fn voting_locking_stake_and_reserving_bond_works() {
ExtBuilder::default().voter_bond(2).build().execute_with(|| {
ExtBuilder::default().voting_bond(2).build().execute_with(|| {
assert_ok!(Elections::submit_candidacy(Origin::signed(5), 0));

assert_eq!(balances(&2), (20, 0));
Expand Down Expand Up @@ -558,7 +558,7 @@ fn retracting_inactive_voter_should_work() {

#[test]
fn retracting_inactive_voter_with_other_candidates_in_slots_should_work() {
ExtBuilder::default().voter_bond(2).build().execute_with(|| {
ExtBuilder::default().voting_bond(2).build().execute_with(|| {
System::set_block_number(4);
assert_ok!(Elections::submit_candidacy(Origin::signed(2), 0));
assert_ok!(Elections::set_approvals(Origin::signed(2), vec![true], 0, 0, 20));
Expand Down Expand Up @@ -1107,7 +1107,7 @@ fn election_present_when_presenter_is_poor_should_not_work() {
let test_present = |p| {
ExtBuilder::default()
.voting_fee(5)
.voter_bond(2)
.voting_bond(2)
.bad_presentation_punishment(p)
.build()
.execute_with(|| {
Expand Down Expand Up @@ -1507,7 +1507,7 @@ fn pot_winning_resets_accumulated_pot() {
#[test]
fn pot_resubmitting_approvals_stores_pot() {
ExtBuilder::default()
.voter_bond(0)
.voting_bond(0)
.voting_fee(0)
.balance_factor(10)
.build()
Expand Down
8 changes: 2 additions & 6 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 All @@ -298,6 +297,8 @@ mod tests {
pub const MaximumBlockWeight: Weight = 1024;
pub const MaximumBlockLength: u32 = 2 * 1024;
pub const AvailableBlockRatio: Perbill = Perbill::one();
pub static Members: Vec<u64> = vec![];
pub static Prime: Option<u64> = None;
}
impl frame_system::Trait for Test {
type BaseCallFilter = ();
Expand Down Expand Up @@ -334,11 +335,6 @@ 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);
}

pub struct TestChangeMembers;
impl ChangeMembers<u64> for TestChangeMembers {
fn change_members_sorted(incoming: &[u64], outgoing: &[u64], new: &[u64]) {
Expand Down
Loading