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
+11 -35
View File
@@ -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;
@@ -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;
@@ -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,
}
@@ -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,
}
}
@@ -209,8 +185,8 @@ 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 {
@@ -218,7 +194,7 @@ impl ExtBuilder {
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);
+6 -6
View File
@@ -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));
@@ -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));
@@ -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));
@@ -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));
@@ -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(|| {
@@ -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()