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
+8 -54
View File
@@ -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.
@@ -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 {}
}
@@ -186,7 +133,14 @@ parameter_types! {
pub const MaximumBlockLength: u32 = 2 * 1024;
pub const AvailableBlockRatio: Perbill = Perbill::one();
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 {
type BaseCallFilter = ();
type Origin = Origin;
@@ -437,7 +391,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);