Tests for referenda.

This commit is contained in:
Gav
2018-02-26 19:06:48 +01:00
parent 2ffcf8ff26
commit 20d81cd1b7
+120 -13
View File
@@ -48,9 +48,7 @@ pub mod public {
.fold((0, 0), |(a, b), (c, d)| (a + c, b + d))
}
/// Propose a sensitive action to be taken. Any action that is enactable by `Proposal` is valid.
/// Proposal is by the `transactor` and will automatically count as an approval. Transactor must
/// be a current validator. It is illegal to propose when there is already a proposal in effect.
/// Propose a sensitive action to be taken.
pub fn propose(validator: &AccountId, proposal: &Proposal) {
if storage::exists(CURRENT_PROPOSAL) {
panic!("there may only be one proposal per era.");
@@ -58,8 +56,7 @@ pub mod public {
storage::put(CURRENT_PROPOSAL, proposal);
}
/// Approve the current era's proposal. Transactor must be a validator. This may not be done more
/// than once for any validator in an era.
/// Vote for or against the proposal.
pub fn vote(who: &AccountId, era_index: BlockNumber, way: bool) {
if era_index != staking::current_era() {
panic!("approval vote applied on non-current era.")
@@ -131,6 +128,7 @@ mod tests {
let dave = Keyring::Dave.to_raw_public();
let eve = Keyring::Eve.to_raw_public();
let ferdie = Keyring::Ferdie.to_raw_public();
let one = Keyring::One.to_raw_public();
map![
twox_128(b"ses:len").to_vec() => vec![].and(&1u64),
@@ -148,6 +146,7 @@ mod tests {
twox_128(&dave.to_keyed_vec(b"sta:bal:")).to_vec() => vec![].and(&40u64),
twox_128(&eve.to_keyed_vec(b"sta:bal:")).to_vec() => vec![].and(&50u64),
twox_128(&ferdie.to_keyed_vec(b"sta:bal:")).to_vec() => vec![].and(&60u64),
twox_128(&one.to_keyed_vec(b"sta:bal:")).to_vec() => vec![].and(&1u64),
twox_128(b"sta:tot").to_vec() => vec![].and(&210u64),
twox_128(b"sta:spe").to_vec() => vec![].and(&1u64),
twox_128(b"sta:vac").to_vec() => vec![].and(&3u64),
@@ -156,25 +155,133 @@ mod tests {
}
#[test]
fn majority_voting_should_work() {
fn simple_passing_should_work() {
let alice = Keyring::Alice.to_raw_public();
let bob = Keyring::Bob.to_raw_public();
let charlie = Keyring::Charlie.to_raw_public();
let dave = Keyring::Dave.to_raw_public();
let eve = Keyring::Eve.to_raw_public();
let ferdie = Keyring::Ferdie.to_raw_public();
let mut t = new_test_ext();
with_externalities(&mut t, || {
assert_eq!(staking::era_length(), 1u64);
assert_eq!(staking::total_stake(), 210u64);
// Block 1: Make proposal. Approve it. Era length changes.
with_env(|e| e.block_number = 1);
public::propose(&alice, &Proposal::StakingSetSessionsPerEra(2));
public::vote(&alice, 1, true);
assert_eq!(public::tally(), (10, 0));
democracy::internal::end_of_an_era();
staking::internal::check_new_era();
assert_eq!(staking::era_length(), 2u64);
});
}
#[test]
fn simple_failing_should_work() {
let alice = Keyring::Alice.to_raw_public();
let mut t = new_test_ext();
with_externalities(&mut t, || {
assert_eq!(staking::era_length(), 1u64);
assert_eq!(staking::total_stake(), 210u64);
with_env(|e| e.block_number = 1);
public::propose(&alice, &Proposal::StakingSetSessionsPerEra(2));
public::vote(&alice, 1, false);
assert_eq!(public::tally(), (0, 10));
democracy::internal::end_of_an_era();
staking::internal::check_new_era();
assert_eq!(staking::era_length(), 1u64);
});
}
#[test]
fn controversial_voting_should_work() {
let alice = Keyring::Alice.to_raw_public();
let bob = Keyring::Bob.to_raw_public();
let charlie = Keyring::Charlie.to_raw_public();
let dave = Keyring::Dave.to_raw_public();
let eve = Keyring::Eve.to_raw_public();
let ferdie = Keyring::Ferdie.to_raw_public();
let one = Keyring::One.to_raw_public();
let mut t = new_test_ext();
with_externalities(&mut t, || {
assert_eq!(staking::era_length(), 1u64);
assert_eq!(staking::total_stake(), 210u64);
with_env(|e| e.block_number = 1);
public::propose(&alice, &Proposal::StakingSetSessionsPerEra(2));
public::vote(&alice, 1, true);
public::vote(&bob, 1, false);
public::vote(&charlie, 1, false);
public::vote(&dave, 1, true);
public::vote(&eve, 1, false);
public::vote(&ferdie, 1, true);
assert_eq!(public::tally(), (60, 0));
assert_eq!(public::tally(), (110, 100));
democracy::internal::end_of_an_era();
staking::internal::check_new_era();
assert_eq!(staking::era_length(), 2u64);
});
}
#[test]
fn controversial_low_turnout_voting_should_work() {
let alice = Keyring::Alice.to_raw_public();
let bob = Keyring::Bob.to_raw_public();
let charlie = Keyring::Charlie.to_raw_public();
let dave = Keyring::Dave.to_raw_public();
let eve = Keyring::Eve.to_raw_public();
let ferdie = Keyring::Ferdie.to_raw_public();
let one = Keyring::One.to_raw_public();
let mut t = new_test_ext();
with_externalities(&mut t, || {
assert_eq!(staking::era_length(), 1u64);
assert_eq!(staking::total_stake(), 210u64);
with_env(|e| e.block_number = 1);
public::propose(&alice, &Proposal::StakingSetSessionsPerEra(2));
public::vote(&eve, 1, false);
public::vote(&ferdie, 1, true);
assert_eq!(public::tally(), (60, 50));
democracy::internal::end_of_an_era();
staking::internal::check_new_era();
assert_eq!(staking::era_length(), 1u64);
});
}
#[test]
fn passing_low_turnout_voting_should_work() {
let alice = Keyring::Alice.to_raw_public();
let bob = Keyring::Bob.to_raw_public();
let charlie = Keyring::Charlie.to_raw_public();
let dave = Keyring::Dave.to_raw_public();
let eve = Keyring::Eve.to_raw_public();
let ferdie = Keyring::Ferdie.to_raw_public();
let one = Keyring::One.to_raw_public();
let mut t = new_test_ext();
with_externalities(&mut t, || {
assert_eq!(staking::era_length(), 1u64);
assert_eq!(staking::total_stake(), 210u64);
with_env(|e| e.block_number = 1);
public::propose(&alice, &Proposal::StakingSetSessionsPerEra(2));
public::vote(&dave, 1, true);
public::vote(&eve, 1, false);
public::vote(&ferdie, 1, true);
assert_eq!(public::tally(), (100, 50));
democracy::internal::end_of_an_era();
staking::internal::check_new_era();