Thread-local parameter_types for testing. (#7542)

* Thread-local parameter_types for testing.

* Better docs.

* Some minors

* Merge'em

* Update frame/support/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Align more to basti's trick

* Update frame/support/src/lib.rs

* Update frame/support/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
Co-authored-by: Bastian Köcher <git@kchr.de>
This commit is contained in:
Kian Paimani
2020-11-20 18:54:19 +01:00
committed by GitHub
parent 52d261e3c7
commit b64b17536a
12 changed files with 128 additions and 217 deletions
@@ -27,10 +27,8 @@ use sp_runtime::{
use sp_core::H256; use sp_core::H256;
use sp_io; use sp_io;
use frame_support::{impl_outer_origin, impl_outer_event, parameter_types}; use frame_support::{impl_outer_origin, impl_outer_event, parameter_types};
use frame_support::traits::Get;
use frame_support::weights::{Weight, DispatchInfo, IdentityFee}; use frame_support::weights::{Weight, DispatchInfo, IdentityFee};
use pallet_transaction_payment::CurrencyAdapter; use pallet_transaction_payment::CurrencyAdapter;
use std::cell::RefCell;
use crate::{GenesisConfig, Module, Trait, decl_tests, tests::CallWithDispatchInfo}; use crate::{GenesisConfig, Module, Trait, decl_tests, tests::CallWithDispatchInfo};
use frame_system as system; use frame_system as system;
@@ -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. // Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
#[derive(Clone, PartialEq, Eq, Debug)] #[derive(Clone, PartialEq, Eq, Debug)]
pub struct Test; pub struct Test;
@@ -66,6 +55,7 @@ parameter_types! {
pub const MaximumBlockWeight: Weight = 1024; pub const MaximumBlockWeight: Weight = 1024;
pub const MaximumBlockLength: u32 = 2 * 1024; pub const MaximumBlockLength: u32 = 2 * 1024;
pub const AvailableBlockRatio: Perbill = Perbill::one(); pub const AvailableBlockRatio: Perbill = Perbill::one();
pub static ExistentialDeposit: u64 = 0;
} }
impl frame_system::Trait for Test { impl frame_system::Trait for Test {
type BaseCallFilter = (); type BaseCallFilter = ();
+2 -11
View File
@@ -27,9 +27,8 @@ use sp_runtime::{
use sp_core::H256; use sp_core::H256;
use sp_io; use sp_io;
use frame_support::{impl_outer_origin, impl_outer_event, parameter_types}; 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 frame_support::weights::{Weight, DispatchInfo, IdentityFee};
use std::cell::RefCell;
use crate::{GenesisConfig, Module, Trait, decl_tests, tests::CallWithDispatchInfo}; use crate::{GenesisConfig, Module, Trait, decl_tests, tests::CallWithDispatchInfo};
use pallet_transaction_payment::CurrencyAdapter; use pallet_transaction_payment::CurrencyAdapter;
@@ -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. // Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
#[derive(Clone, PartialEq, Eq, Debug)] #[derive(Clone, PartialEq, Eq, Debug)]
pub struct Test; pub struct Test;
@@ -66,6 +56,7 @@ parameter_types! {
pub const MaximumBlockWeight: Weight = 1024; pub const MaximumBlockWeight: Weight = 1024;
pub const MaximumBlockLength: u32 = 2 * 1024; pub const MaximumBlockLength: u32 = 2 * 1024;
pub const AvailableBlockRatio: Perbill = Perbill::one(); pub const AvailableBlockRatio: Perbill = Perbill::one();
pub static ExistentialDeposit: u64 = 0;
} }
impl frame_system::Trait for Test { impl frame_system::Trait for Test {
type BaseCallFilter = (); type BaseCallFilter = ();
+2 -11
View File
@@ -30,11 +30,10 @@ use sp_runtime::{
use frame_support::{ use frame_support::{
assert_ok, assert_err_ignore_postinfo, impl_outer_dispatch, impl_outer_event, assert_ok, assert_err_ignore_postinfo, impl_outer_dispatch, impl_outer_event,
impl_outer_origin, parameter_types, StorageMap, StorageValue, impl_outer_origin, parameter_types, StorageMap, StorageValue,
traits::{Currency, Get, ReservableCurrency}, traits::{Currency, ReservableCurrency},
weights::{Weight, PostDispatchInfo}, weights::{Weight, PostDispatchInfo},
dispatch::DispatchErrorWithPostInfo, dispatch::DispatchErrorWithPostInfo,
}; };
use std::cell::RefCell;
use frame_system::{self as system, EventRecord, Phase}; use frame_system::{self as system, EventRecord, Phase};
mod contracts { mod contracts {
@@ -99,15 +98,6 @@ 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)] #[derive(Clone, Eq, PartialEq, Debug)]
pub struct Test; pub struct Test;
parameter_types! { parameter_types! {
@@ -115,6 +105,7 @@ parameter_types! {
pub const MaximumBlockWeight: Weight = 1024; pub const MaximumBlockWeight: Weight = 1024;
pub const MaximumBlockLength: u32 = 2 * 1024; pub const MaximumBlockLength: u32 = 2 * 1024;
pub const AvailableBlockRatio: Perbill = Perbill::one(); pub const AvailableBlockRatio: Perbill = Perbill::one();
pub static ExistentialDeposit: u64 = 0;
} }
impl frame_system::Trait for Test { impl frame_system::Trait for Test {
type BaseCallFilter = (); type BaseCallFilter = ();
+3 -13
View File
@@ -18,7 +18,6 @@
//! The crate's tests. //! The crate's tests.
use super::*; use super::*;
use std::cell::RefCell;
use codec::Encode; use codec::Encode;
use frame_support::{ use frame_support::{
impl_outer_origin, impl_outer_dispatch, assert_noop, assert_ok, parameter_types, impl_outer_origin, impl_outer_dispatch, assert_noop, assert_ok, parameter_types,
@@ -154,6 +153,8 @@ parameter_types! {
pub const CooloffPeriod: u64 = 2; pub const CooloffPeriod: u64 = 2;
pub const MaxVotes: u32 = 100; pub const MaxVotes: u32 = 100;
pub const MaxProposals: u32 = MAX_PROPOSALS; pub const MaxProposals: u32 = MAX_PROPOSALS;
pub static PreimageByteDeposit: u64 = 0;
pub static InstantAllowed: bool = false;
} }
ord_parameter_types! { ord_parameter_types! {
pub const One: u64 = 1; pub const One: u64 = 1;
@@ -171,18 +172,7 @@ impl Contains<u64> for OneToFive {
#[cfg(feature = "runtime-benchmarks")] #[cfg(feature = "runtime-benchmarks")]
fn add(_m: &u64) {} 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 { impl super::Trait for Test {
type Proposal = Call; type Proposal = Call;
type Event = Event; type Event = Event;
+7 -31
View File
@@ -1058,7 +1058,6 @@ impl<T: Trait> ContainsLengthBound for Module<T> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use std::cell::RefCell;
use frame_support::{assert_ok, assert_noop, assert_err_with_weight, parameter_types, use frame_support::{assert_ok, assert_noop, assert_err_with_weight, parameter_types,
weights::Weight, weights::Weight,
}; };
@@ -1123,36 +1122,13 @@ mod tests {
pub const CandidacyBond: u64 = 3; pub const CandidacyBond: u64 = 3;
} }
thread_local! { frame_support::parameter_types! {
static VOTING_BOND: RefCell<u64> = RefCell::new(2); pub static VotingBond: u64 = 2;
static DESIRED_MEMBERS: RefCell<u32> = RefCell::new(2); pub static DesiredMembers: u32 = 2;
static DESIRED_RUNNERS_UP: RefCell<u32> = RefCell::new(2); pub static DesiredRunnersUp: u32 = 2;
static TERM_DURATION: RefCell<u64> = RefCell::new(5); pub static TermDuration: u64 = 5;
} pub static Members: Vec<u64> = vec![];
pub static Prime: Option<u64> = None;
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);
} }
pub struct TestChangeMembers; pub struct TestChangeMembers;
+11 -35
View File
@@ -19,10 +19,9 @@
#![cfg(test)] #![cfg(test)]
use std::cell::RefCell;
use frame_support::{ use frame_support::{
StorageValue, StorageMap, parameter_types, assert_ok, StorageValue, StorageMap, parameter_types, assert_ok,
traits::{Get, ChangeMembers, Currency, LockIdentifier}, traits::{ChangeMembers, Currency, LockIdentifier},
weights::Weight, weights::Weight,
}; };
use sp_core::H256; use sp_core::H256;
@@ -85,34 +84,11 @@ parameter_types! {
pub const InactiveGracePeriod: u32 = 1; pub const InactiveGracePeriod: u32 = 1;
pub const VotingPeriod: u64 = 4; pub const VotingPeriod: u64 = 4;
pub const MinimumVotingLock: u64 = 5; pub const MinimumVotingLock: u64 = 5;
} pub static VotingBond: u64 = 0;
pub static VotingFee: u64 = 0;
thread_local! { pub static PresentSlashPerVoter: u64 = 0;
static VOTER_BOND: RefCell<u64> = RefCell::new(0); pub static DecayRatio: u32 = 0;
static VOTING_FEE: RefCell<u64> = RefCell::new(0); pub static Members: Vec<u64> = vec![];
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 struct TestChangeMembers; pub struct TestChangeMembers;
@@ -175,7 +151,7 @@ pub struct ExtBuilder {
decay_ratio: u32, decay_ratio: u32,
desired_seats: u32, desired_seats: u32,
voting_fee: u64, voting_fee: u64,
voter_bond: u64, voting_bond: u64,
bad_presentation_punishment: u64, bad_presentation_punishment: u64,
} }
@@ -186,7 +162,7 @@ impl Default for ExtBuilder {
decay_ratio: 24, decay_ratio: 24,
desired_seats: 2, desired_seats: 2,
voting_fee: 0, voting_fee: 0,
voter_bond: 0, voting_bond: 0,
bad_presentation_punishment: 1, bad_presentation_punishment: 1,
} }
} }
@@ -209,8 +185,8 @@ impl ExtBuilder {
self.bad_presentation_punishment = fee; self.bad_presentation_punishment = fee;
self self
} }
pub fn voter_bond(mut self, fee: u64) -> Self { pub fn voting_bond(mut self, fee: u64) -> Self {
self.voter_bond = fee; self.voting_bond = fee;
self self
} }
pub fn desired_seats(mut self, seats: u32) -> Self { pub fn desired_seats(mut self, seats: u32) -> Self {
@@ -218,7 +194,7 @@ impl ExtBuilder {
self self
} }
pub fn build(self) -> sp_io::TestExternalities { 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); VOTING_FEE.with(|v| *v.borrow_mut() = self.voting_fee);
PRESENT_SLASH_PER_VOTER.with(|v| *v.borrow_mut() = self.bad_presentation_punishment); PRESENT_SLASH_PER_VOTER.with(|v| *v.borrow_mut() = self.bad_presentation_punishment);
DECAY_RATIO.with(|v| *v.borrow_mut() = self.decay_ratio); DECAY_RATIO.with(|v| *v.borrow_mut() = self.decay_ratio);
+6 -6
View File
@@ -298,7 +298,7 @@ fn voting_initial_set_approvals_ignores_voter_index() {
} }
#[test] #[test]
fn voting_bad_approval_index_slashes_voters_and_bond_reduces_stake() { 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)); assert_ok!(Elections::submit_candidacy(Origin::signed(2), 0));
(1..=63).for_each(|i| vote(i, 0)); (1..=63).for_each(|i| vote(i, 0));
@@ -365,7 +365,7 @@ fn voting_cannot_lock_less_than_limit() {
#[test] #[test]
fn voting_locking_more_than_total_balance_is_moot() { 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_ok!(Elections::submit_candidacy(Origin::signed(2), 0));
assert_eq!(balances(&3), (30, 0)); assert_eq!(balances(&3), (30, 0));
@@ -381,7 +381,7 @@ fn voting_locking_more_than_total_balance_is_moot() {
#[test] #[test]
fn voting_locking_stake_and_reserving_bond_works() { 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_ok!(Elections::submit_candidacy(Origin::signed(5), 0));
assert_eq!(balances(&2), (20, 0)); assert_eq!(balances(&2), (20, 0));
@@ -558,7 +558,7 @@ fn retracting_inactive_voter_should_work() {
#[test] #[test]
fn retracting_inactive_voter_with_other_candidates_in_slots_should_work() { 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); System::set_block_number(4);
assert_ok!(Elections::submit_candidacy(Origin::signed(2), 0)); assert_ok!(Elections::submit_candidacy(Origin::signed(2), 0));
assert_ok!(Elections::set_approvals(Origin::signed(2), vec![true], 0, 0, 20)); assert_ok!(Elections::set_approvals(Origin::signed(2), vec![true], 0, 0, 20));
@@ -1107,7 +1107,7 @@ fn election_present_when_presenter_is_poor_should_not_work() {
let test_present = |p| { let test_present = |p| {
ExtBuilder::default() ExtBuilder::default()
.voting_fee(5) .voting_fee(5)
.voter_bond(2) .voting_bond(2)
.bad_presentation_punishment(p) .bad_presentation_punishment(p)
.build() .build()
.execute_with(|| { .execute_with(|| {
@@ -1507,7 +1507,7 @@ fn pot_winning_resets_accumulated_pot() {
#[test] #[test]
fn pot_resubmitting_approvals_stores_pot() { fn pot_resubmitting_approvals_stores_pot() {
ExtBuilder::default() ExtBuilder::default()
.voter_bond(0) .voting_bond(0)
.voting_fee(0) .voting_fee(0)
.balance_factor(10) .balance_factor(10)
.build() .build()
+2 -6
View File
@@ -278,7 +278,6 @@ impl<T: Trait<I>, I: Instance> Contains<T::AccountId> for Module<T, I> {
mod tests { mod tests {
use super::*; use super::*;
use std::cell::RefCell;
use frame_support::{ use frame_support::{
assert_ok, assert_noop, impl_outer_origin, parameter_types, weights::Weight, assert_ok, assert_noop, impl_outer_origin, parameter_types, weights::Weight,
ord_parameter_types ord_parameter_types
@@ -298,6 +297,8 @@ mod tests {
pub const MaximumBlockWeight: Weight = 1024; pub const MaximumBlockWeight: Weight = 1024;
pub const MaximumBlockLength: u32 = 2 * 1024; pub const MaximumBlockLength: u32 = 2 * 1024;
pub const AvailableBlockRatio: Perbill = Perbill::one(); pub const AvailableBlockRatio: Perbill = Perbill::one();
pub static Members: Vec<u64> = vec![];
pub static Prime: Option<u64> = None;
} }
impl frame_system::Trait for Test { impl frame_system::Trait for Test {
type BaseCallFilter = (); type BaseCallFilter = ();
@@ -334,11 +335,6 @@ mod tests {
pub const Five: u64 = 5; 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; pub struct TestChangeMembers;
impl ChangeMembers<u64> for TestChangeMembers { impl ChangeMembers<u64> for TestChangeMembers {
fn change_members_sorted(incoming: &[u64], outgoing: &[u64], new: &[u64]) { fn change_members_sorted(incoming: &[u64], outgoing: &[u64], new: &[u64]) {
+8 -54
View File
@@ -47,12 +47,6 @@ pub(crate) type Balance = u128;
thread_local! { thread_local! {
static SESSION: RefCell<(Vec<AccountId>, HashSet<AccountId>)> = RefCell::new(Default::default()); 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. /// Another session handler struct to test on_disabled.
@@ -92,53 +86,6 @@ pub fn is_disabled(controller: AccountId) -> bool {
SESSION.with(|d| d.borrow().1.contains(&stash)) 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! { impl_outer_origin! {
pub enum Origin for Test where system = frame_system {} pub enum Origin for Test where system = frame_system {}
} }
@@ -186,7 +133,14 @@ parameter_types! {
pub const MaximumBlockLength: u32 = 2 * 1024; pub const MaximumBlockLength: u32 = 2 * 1024;
pub const AvailableBlockRatio: Perbill = Perbill::one(); pub const AvailableBlockRatio: Perbill = Perbill::one();
pub const MaxLocks: u32 = 1024; pub const MaxLocks: u32 = 1024;
pub static SessionsPerEra: SessionIndex = 3;
pub static ExistentialDeposit: Balance = 0;
pub static SlashDeferDuration: EraIndex = 0;
pub static ElectionLookahead: BlockNumber = 0;
pub static Period: BlockNumber = 1;
pub static MaxIterations: u32 = 0;
} }
impl frame_system::Trait for Test { impl frame_system::Trait for Test {
type BaseCallFilter = (); type BaseCallFilter = ();
type Origin = Origin; type Origin = Origin;
@@ -437,7 +391,7 @@ impl ExtBuilder {
pub fn set_associated_constants(&self) { pub fn set_associated_constants(&self) {
EXISTENTIAL_DEPOSIT.with(|v| *v.borrow_mut() = self.existential_deposit); EXISTENTIAL_DEPOSIT.with(|v| *v.borrow_mut() = self.existential_deposit);
SLASH_DEFER_DURATION.with(|v| *v.borrow_mut() = self.slash_defer_duration); 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); ELECTION_LOOKAHEAD.with(|v| *v.borrow_mut() = self.election_lookahead);
PERIOD.with(|v| *v.borrow_mut() = self.session_length); PERIOD.with(|v| *v.borrow_mut() = self.session_length);
MAX_ITERATIONS.with(|v| *v.borrow_mut() = self.max_offchain_iterations); MAX_ITERATIONS.with(|v| *v.borrow_mut() = self.max_offchain_iterations);
+81 -9
View File
@@ -85,21 +85,27 @@ pub enum Never {}
/// Create new implementations of the [`Get`](crate::traits::Get) trait. /// Create new implementations of the [`Get`](crate::traits::Get) trait.
/// ///
/// The so-called parameter type can be created in three different ways: /// The so-called parameter type can be created in four different ways:
/// ///
/// - Using `const` to create a parameter type that provides a `const` getter. /// - Using `const` to create a parameter type that provides a `const` getter. It is required that
/// It is required that the `value` is const. /// the `value` is const.
/// ///
/// - Declare the parameter type without `const` to have more freedom when creating the value. /// - Declare the parameter type without `const` to have more freedom when creating the value.
/// ///
/// - Using `storage` to create a storage parameter type. This type is special as it tries to /// - Using `storage` to create a storage parameter type. This type is special as it tries to load
/// load the value from the storage under a fixed key. If the value could not be found in the /// the value from the storage under a fixed key. If the value could not be found in the storage,
/// storage, the given default value will be returned. It is required that the value implements /// the given default value will be returned. It is required that the value implements
/// [`Encode`](codec::Encode) and [`Decode`](codec::Decode). The key for looking up the value /// [`Encode`](codec::Encode) and [`Decode`](codec::Decode). The key for looking up the value in
/// in the storage is built using the following formular: /// the storage is built using the following formula:
/// ///
/// `twox_128(":" ++ NAME ++ ":")` where `NAME` is the name that is passed as type name. /// `twox_128(":" ++ NAME ++ ":")` where `NAME` is the name that is passed as type name.
/// ///
/// - Using `static` to create a static parameter type. Its value is
/// being provided by a static variable with the equivalent name in `UPPER_SNAKE_CASE`. An
/// additional `set` function is provided in this case to alter the static variable.
///
/// **This is intended for testing ONLY and is ONLY available when `std` is enabled**
///
/// # Examples /// # Examples
/// ///
/// ``` /// ```
@@ -114,12 +120,14 @@ pub enum Never {}
/// /// Visibility of the type is optional /// /// Visibility of the type is optional
/// OtherArgument: u64 = non_const_expression(); /// OtherArgument: u64 = non_const_expression();
/// pub storage StorageArgument: u64 = 5; /// pub storage StorageArgument: u64 = 5;
/// pub static StaticArgument: u32 = 7;
/// } /// }
/// ///
/// trait Config { /// trait Config {
/// type Parameter: Get<u64>; /// type Parameter: Get<u64>;
/// type OtherParameter: Get<u64>; /// type OtherParameter: Get<u64>;
/// type StorageParameter: Get<u64>; /// type StorageParameter: Get<u64>;
/// type StaticParameter: Get<u32>;
/// } /// }
/// ///
/// struct Runtime; /// struct Runtime;
@@ -127,7 +135,10 @@ pub enum Never {}
/// type Parameter = Argument; /// type Parameter = Argument;
/// type OtherParameter = OtherArgument; /// type OtherParameter = OtherArgument;
/// type StorageParameter = StorageArgument; /// type StorageParameter = StorageArgument;
/// type StaticParameter = StaticArgument;
/// } /// }
///
/// // In testing, `StaticArgument` can be altered later: `StaticArgument::set(8)`.
/// ``` /// ```
/// ///
/// # Invalid example: /// # Invalid example:
@@ -142,7 +153,6 @@ pub enum Never {}
/// pub const Argument: u64 = non_const_expression(); /// pub const Argument: u64 = non_const_expression();
/// } /// }
/// ``` /// ```
#[macro_export] #[macro_export]
macro_rules! parameter_types { macro_rules! parameter_types {
( (
@@ -235,7 +245,69 @@ macro_rules! parameter_types {
I::from(Self::get()) I::from(Self::get())
} }
} }
};
(
$(
$( #[ $attr:meta ] )*
$vis:vis static $name:ident: $type:ty = $value:expr;
)*
) => (
$crate::parameter_types_impl_thread_local!(
$(
$( #[ $attr ] )*
$vis static $name: $type = $value;
)*
);
);
}
#[cfg(not(feature = "std"))]
#[macro_export]
macro_rules! parameter_types_impl_thread_local {
( $( $any:tt )* ) => {
compile_error!("static parameter types is only available in std and for testing.");
};
}
#[cfg(feature = "std")]
#[macro_export]
macro_rules! parameter_types_impl_thread_local {
(
$(
$( #[ $attr:meta ] )*
$vis:vis static $name:ident: $type:ty = $value:expr;
)*
) => {
$crate::parameter_types_impl_thread_local!(
IMPL_THREAD_LOCAL $( $vis, $name, $type, $value, )*
);
$crate::paste::item! {
$crate::parameter_types!(
$(
$( #[ $attr ] )*
$vis $name: $type = [<$name:snake:upper>].with(|v| v.borrow().clone());
)*
);
$(
impl $name {
/// Set the internal value.
pub fn set(t: $type) {
[<$name:snake:upper>].with(|v| *v.borrow_mut() = t);
} }
}
)*
}
};
(IMPL_THREAD_LOCAL $( $vis:vis, $name:ident, $type:ty, $value:expr, )* ) => {
$crate::paste::item! {
thread_local! {
$(
pub static [<$name:snake:upper>]: std::cell::RefCell<$type> =
std::cell::RefCell::new($value);
)*
}
}
};
} }
/// Macro for easily creating a new implementation of both the `Get` and `Contains` traits. Use /// Macro for easily creating a new implementation of both the `Get` and `Contains` traits. Use
+3 -20
View File
@@ -571,7 +571,6 @@ mod tests {
traits::{BlakeTwo256, IdentityLookup}, traits::{BlakeTwo256, IdentityLookup},
Perbill, Perbill,
}; };
use std::cell::RefCell;
use smallvec::smallvec; use smallvec::smallvec;
const CALL: &<Runtime as frame_system::Trait>::Call = const CALL: &<Runtime as frame_system::Trait>::Call =
@@ -599,20 +598,14 @@ mod tests {
pub enum Origin for Runtime {} pub enum Origin for Runtime {}
} }
thread_local! {
static EXTRINSIC_BASE_WEIGHT: RefCell<u64> = RefCell::new(0);
}
pub struct ExtrinsicBaseWeight;
impl Get<u64> for ExtrinsicBaseWeight {
fn get() -> u64 { EXTRINSIC_BASE_WEIGHT.with(|v| *v.borrow()) }
}
parameter_types! { parameter_types! {
pub const BlockHashCount: u64 = 250; pub const BlockHashCount: u64 = 250;
pub const MaximumBlockWeight: Weight = 1024; pub const MaximumBlockWeight: Weight = 1024;
pub const MaximumBlockLength: u32 = 2 * 1024; pub const MaximumBlockLength: u32 = 2 * 1024;
pub const AvailableBlockRatio: Perbill = Perbill::one(); pub const AvailableBlockRatio: Perbill = Perbill::one();
pub static ExtrinsicBaseWeight: u64 = 0;
pub static TransactionByteFee: u64 = 1;
pub static WeightToFee: u64 = 1;
} }
impl frame_system::Trait for Runtime { impl frame_system::Trait for Runtime {
@@ -656,17 +649,7 @@ mod tests {
type MaxLocks = (); type MaxLocks = ();
type WeightInfo = (); type WeightInfo = ();
} }
thread_local! {
static TRANSACTION_BYTE_FEE: RefCell<u64> = RefCell::new(1);
static WEIGHT_TO_FEE: RefCell<u64> = RefCell::new(1);
}
pub struct TransactionByteFee;
impl Get<u64> for TransactionByteFee {
fn get() -> u64 { TRANSACTION_BYTE_FEE.with(|v| *v.borrow()) }
}
pub struct WeightToFee;
impl WeightToFeePolynomial for WeightToFee { impl WeightToFeePolynomial for WeightToFee {
type Balance = u64; type Balance = u64;
+1 -9
View File
@@ -390,10 +390,8 @@ impl<T: Trait> VestingSchedule<T::AccountId> for Module<T> where
mod tests { mod tests {
use super::*; use super::*;
use std::cell::RefCell;
use frame_support::{ use frame_support::{
assert_ok, assert_noop, impl_outer_origin, parameter_types, weights::Weight, assert_ok, assert_noop, impl_outer_origin, parameter_types, weights::Weight,
traits::Get
}; };
use sp_core::H256; use sp_core::H256;
use sp_runtime::{ use sp_runtime::{
@@ -456,6 +454,7 @@ mod tests {
} }
parameter_types! { parameter_types! {
pub const MinVestedTransfer: u64 = 256 * 2; pub const MinVestedTransfer: u64 = 256 * 2;
pub static ExistentialDeposit: u64 = 0;
} }
impl Trait for Test { impl Trait for Test {
type Event = (); type Event = ();
@@ -468,13 +467,6 @@ mod tests {
type Balances = pallet_balances::Module<Test>; type Balances = pallet_balances::Module<Test>;
type Vesting = Module<Test>; type Vesting = Module<Test>;
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()) }
}
pub struct ExtBuilder { pub struct ExtBuilder {
existential_deposit: u64, existential_deposit: u64,