Redesign Democracy pallet (#5294)

* Repot a bit of democracy code

* Basic logic is drafted

* Lazy democracy builds.

* Add non-locked split-voting and instant-scheduling.

* Introduce delegation that works.

* Builds again.

* Indentation

* Building.

* Docs and migration

* Fix half of the tests

* Fix up & repot tests

* Fix runtime build

* Update docs

* Docs

* Nits.

* Turnout counts full capital

* Delegations could towards capital

* proxy delegation & proxy unvoting

* Fix

* Tests for split-voting

* Add missing file

* Persistent locking.
This commit is contained in:
Gavin Wood
2020-03-21 16:08:48 +01:00
committed by GitHub
parent 40f57f8ffa
commit 22f88bf9d1
19 changed files with 3080 additions and 1821 deletions
@@ -0,0 +1,94 @@
// Copyright 2017-2020 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! The tests for cancelation functionality.
use super::*;
#[test]
fn cancel_referendum_should_work() {
new_test_ext().execute_with(|| {
System::set_block_number(1);
let r = Democracy::inject_referendum(
2,
set_balance_proposal_hash_and_note(2),
VoteThreshold::SuperMajorityApprove,
0
);
assert_ok!(Democracy::vote(Origin::signed(1), r, aye(1)));
assert_ok!(Democracy::cancel_referendum(Origin::ROOT, r.into()));
next_block();
next_block();
assert_eq!(Balances::free_balance(42), 0);
});
}
#[test]
fn cancel_queued_should_work() {
new_test_ext().execute_with(|| {
System::set_block_number(0);
assert_ok!(propose_set_balance_and_note(1, 2, 1));
// start of 2 => next referendum scheduled.
fast_forward_to(2);
assert_ok!(Democracy::vote(Origin::signed(1), 0, aye(1)));
fast_forward_to(4);
assert_eq!(Democracy::dispatch_queue(), vec![
(6, set_balance_proposal_hash_and_note(2), 0)
]);
assert_noop!(Democracy::cancel_queued(Origin::ROOT, 1), Error::<Test>::ProposalMissing);
assert_ok!(Democracy::cancel_queued(Origin::ROOT, 0));
assert_eq!(Democracy::dispatch_queue(), vec![]);
});
}
#[test]
fn emergency_cancel_should_work() {
new_test_ext().execute_with(|| {
System::set_block_number(0);
let r = Democracy::inject_referendum(
2,
set_balance_proposal_hash_and_note(2),
VoteThreshold::SuperMajorityApprove,
2
);
assert!(Democracy::referendum_status(r).is_ok());
assert_noop!(Democracy::emergency_cancel(Origin::signed(3), r), BadOrigin);
assert_ok!(Democracy::emergency_cancel(Origin::signed(4), r));
assert!(Democracy::referendum_info(r).is_none());
// some time later...
let r = Democracy::inject_referendum(
2,
set_balance_proposal_hash_and_note(2),
VoteThreshold::SuperMajorityApprove,
2
);
assert!(Democracy::referendum_status(r).is_ok());
assert_noop!(
Democracy::emergency_cancel(Origin::signed(4), r),
Error::<Test>::AlreadyCanceled,
);
});
}
@@ -0,0 +1,178 @@
// Copyright 2017-2020 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! The tests for functionality concerning delegation.
use super::*;
#[test]
fn single_proposal_should_work_with_delegation() {
new_test_ext().execute_with(|| {
System::set_block_number(0);
assert_ok!(propose_set_balance_and_note(1, 2, 1));
fast_forward_to(2);
// Delegate first vote.
assert_ok!(Democracy::delegate(Origin::signed(2), 1, Conviction::None, 20));
let r = 0;
assert_ok!(Democracy::vote(Origin::signed(1), r, aye(1)));
assert_eq!(tally(r), Tally { ayes: 3, nays: 0, turnout: 30 });
// Delegate a second vote.
assert_ok!(Democracy::delegate(Origin::signed(3), 1, Conviction::None, 30));
assert_eq!(tally(r), Tally { ayes: 6, nays: 0, turnout: 60 });
// Reduce first vote.
assert_ok!(Democracy::delegate(Origin::signed(2), 1, Conviction::None, 10));
assert_eq!(tally(r), Tally { ayes: 5, nays: 0, turnout: 50 });
// Second vote delegates to first; we don't do tiered delegation, so it doesn't get used.
assert_ok!(Democracy::delegate(Origin::signed(3), 2, Conviction::None, 30));
assert_eq!(tally(r), Tally { ayes: 2, nays: 0, turnout: 20 });
// Main voter cancels their vote
assert_ok!(Democracy::remove_vote(Origin::signed(1), r));
assert_eq!(tally(r), Tally { ayes: 0, nays: 0, turnout: 0 });
// First delegator delegates half funds with conviction; nothing changes yet.
assert_ok!(Democracy::delegate(Origin::signed(2), 1, Conviction::Locked1x, 10));
assert_eq!(tally(r), Tally { ayes: 0, nays: 0, turnout: 0 });
// Main voter reinstates their vote
assert_ok!(Democracy::vote(Origin::signed(1), r, aye(1)));
assert_eq!(tally(r), Tally { ayes: 11, nays: 0, turnout: 20 });
});
}
#[test]
fn self_delegation_not_allowed() {
new_test_ext().execute_with(|| {
assert_noop!(
Democracy::delegate(Origin::signed(1), 1, Conviction::None, 10),
Error::<Test>::Nonsense,
);
});
}
#[test]
fn cyclic_delegation_should_unwind() {
new_test_ext().execute_with(|| {
System::set_block_number(0);
assert_ok!(propose_set_balance_and_note(1, 2, 1));
fast_forward_to(2);
// Check behavior with cycle.
assert_ok!(Democracy::delegate(Origin::signed(2), 1, Conviction::None, 20));
assert_ok!(Democracy::delegate(Origin::signed(3), 2, Conviction::None, 30));
assert_ok!(Democracy::delegate(Origin::signed(1), 3, Conviction::None, 10));
let r = 0;
assert_ok!(Democracy::undelegate(Origin::signed(3)));
assert_ok!(Democracy::vote(Origin::signed(3), r, aye(3)));
assert_ok!(Democracy::undelegate(Origin::signed(1)));
assert_ok!(Democracy::vote(Origin::signed(1), r, nay(1)));
// Delegated vote is counted.
assert_eq!(tally(r), Tally { ayes: 3, nays: 3, turnout: 60 });
});
}
#[test]
fn single_proposal_should_work_with_vote_and_delegation() {
// If transactor already voted, delegated vote is overwritten.
new_test_ext().execute_with(|| {
System::set_block_number(0);
assert_ok!(propose_set_balance_and_note(1, 2, 1));
fast_forward_to(2);
let r = 0;
assert_ok!(Democracy::vote(Origin::signed(1), r, aye(1)));
assert_ok!(Democracy::vote(Origin::signed(2), r, nay(2)));
assert_eq!(tally(r), Tally { ayes: 1, nays: 2, turnout: 30 });
// Delegate vote.
assert_ok!(Democracy::remove_vote(Origin::signed(2), r));
assert_ok!(Democracy::delegate(Origin::signed(2), 1, Conviction::None, 20));
// Delegated vote replaces the explicit vote.
assert_eq!(tally(r), Tally { ayes: 3, nays: 0, turnout: 30 });
});
}
#[test]
fn single_proposal_should_work_with_undelegation() {
new_test_ext().execute_with(|| {
System::set_block_number(0);
assert_ok!(propose_set_balance_and_note(1, 2, 1));
// Delegate and undelegate vote.
assert_ok!(Democracy::delegate(Origin::signed(2), 1, Conviction::None, 20));
assert_ok!(Democracy::undelegate(Origin::signed(2)));
fast_forward_to(2);
let r = 0;
assert_ok!(Democracy::vote(Origin::signed(1), r, aye(1)));
// Delegated vote is not counted.
assert_eq!(tally(r), Tally { ayes: 1, nays: 0, turnout: 10 });
});
}
#[test]
fn single_proposal_should_work_with_delegation_and_vote() {
// If transactor voted, delegated vote is overwritten.
new_test_ext().execute_with(|| {
let r = begin_referendum();
// Delegate, undelegate and vote.
assert_ok!(Democracy::vote(Origin::signed(1), r, aye(1)));
assert_ok!(Democracy::delegate(Origin::signed(2), 1, Conviction::None, 20));
assert_eq!(tally(r), Tally { ayes: 3, nays: 0, turnout: 30 });
assert_ok!(Democracy::undelegate(Origin::signed(2)));
assert_ok!(Democracy::vote(Origin::signed(2), r, aye(2)));
// Delegated vote is not counted.
assert_eq!(tally(r), Tally { ayes: 3, nays: 0, turnout: 30 });
});
}
#[test]
fn conviction_should_be_honored_in_delegation() {
// If transactor voted, delegated vote is overwritten.
new_test_ext().execute_with(|| {
let r = begin_referendum();
// Delegate, undelegate and vote.
assert_ok!(Democracy::delegate(Origin::signed(2), 1, Conviction::Locked6x, 20));
assert_ok!(Democracy::vote(Origin::signed(1), r, aye(1)));
// Delegated vote is huge.
assert_eq!(tally(r), Tally { ayes: 121, nays: 0, turnout: 30 });
});
}
#[test]
fn split_vote_delegation_should_be_ignored() {
// If transactor voted, delegated vote is overwritten.
new_test_ext().execute_with(|| {
let r = begin_referendum();
assert_ok!(Democracy::delegate(Origin::signed(2), 1, Conviction::Locked6x, 20));
assert_ok!(Democracy::vote(Origin::signed(1), r, AccountVote::Split { aye: 10, nay: 0 }));
// Delegated vote is huge.
assert_eq!(tally(r), Tally { ayes: 1, nays: 0, turnout: 10 });
});
}
@@ -0,0 +1,289 @@
// Copyright 2017-2020 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! The tests for functionality concerning the "external" origin.
use super::*;
#[test]
fn veto_external_works() {
new_test_ext().execute_with(|| {
System::set_block_number(0);
assert_ok!(Democracy::external_propose(
Origin::signed(2),
set_balance_proposal_hash_and_note(2),
));
assert!(<NextExternal<Test>>::exists());
let h = set_balance_proposal_hash_and_note(2);
assert_ok!(Democracy::veto_external(Origin::signed(3), h.clone()));
// cancelled.
assert!(!<NextExternal<Test>>::exists());
// fails - same proposal can't be resubmitted.
assert_noop!(Democracy::external_propose(
Origin::signed(2),
set_balance_proposal_hash(2),
), Error::<Test>::ProposalBlacklisted);
fast_forward_to(1);
// fails as we're still in cooloff period.
assert_noop!(Democracy::external_propose(
Origin::signed(2),
set_balance_proposal_hash(2),
), Error::<Test>::ProposalBlacklisted);
fast_forward_to(2);
// works; as we're out of the cooloff period.
assert_ok!(Democracy::external_propose(
Origin::signed(2),
set_balance_proposal_hash_and_note(2),
));
assert!(<NextExternal<Test>>::exists());
// 3 can't veto the same thing twice.
assert_noop!(
Democracy::veto_external(Origin::signed(3), h.clone()),
Error::<Test>::AlreadyVetoed
);
// 4 vetoes.
assert_ok!(Democracy::veto_external(Origin::signed(4), h.clone()));
// cancelled again.
assert!(!<NextExternal<Test>>::exists());
fast_forward_to(3);
// same proposal fails as we're still in cooloff
assert_noop!(Democracy::external_propose(
Origin::signed(2),
set_balance_proposal_hash(2),
), Error::<Test>::ProposalBlacklisted);
// different proposal works fine.
assert_ok!(Democracy::external_propose(
Origin::signed(2),
set_balance_proposal_hash_and_note(3),
));
});
}
#[test]
fn external_referendum_works() {
new_test_ext().execute_with(|| {
System::set_block_number(0);
assert_noop!(
Democracy::external_propose(
Origin::signed(1),
set_balance_proposal_hash(2),
),
BadOrigin,
);
assert_ok!(Democracy::external_propose(
Origin::signed(2),
set_balance_proposal_hash_and_note(2),
));
assert_noop!(Democracy::external_propose(
Origin::signed(2),
set_balance_proposal_hash(1),
), Error::<Test>::DuplicateProposal);
fast_forward_to(2);
assert_eq!(
Democracy::referendum_status(0),
Ok(ReferendumStatus {
end: 4,
proposal_hash: set_balance_proposal_hash(2),
threshold: VoteThreshold::SuperMajorityApprove,
delay: 2,
tally: Tally { ayes: 0, nays: 0, turnout: 0 },
})
);
});
}
#[test]
fn external_majority_referendum_works() {
new_test_ext().execute_with(|| {
System::set_block_number(0);
assert_noop!(
Democracy::external_propose_majority(
Origin::signed(1),
set_balance_proposal_hash(2)
),
BadOrigin,
);
assert_ok!(Democracy::external_propose_majority(
Origin::signed(3),
set_balance_proposal_hash_and_note(2)
));
fast_forward_to(2);
assert_eq!(
Democracy::referendum_status(0),
Ok(ReferendumStatus {
end: 4,
proposal_hash: set_balance_proposal_hash(2),
threshold: VoteThreshold::SimpleMajority,
delay: 2,
tally: Tally { ayes: 0, nays: 0, turnout: 0 },
})
);
});
}
#[test]
fn external_default_referendum_works() {
new_test_ext().execute_with(|| {
System::set_block_number(0);
assert_noop!(
Democracy::external_propose_default(
Origin::signed(3),
set_balance_proposal_hash(2)
),
BadOrigin,
);
assert_ok!(Democracy::external_propose_default(
Origin::signed(1),
set_balance_proposal_hash_and_note(2)
));
fast_forward_to(2);
assert_eq!(
Democracy::referendum_status(0),
Ok(ReferendumStatus {
end: 4,
proposal_hash: set_balance_proposal_hash(2),
threshold: VoteThreshold::SuperMajorityAgainst,
delay: 2,
tally: Tally { ayes: 0, nays: 0, turnout: 0 },
})
);
});
}
#[test]
fn external_and_public_interleaving_works() {
new_test_ext().execute_with(|| {
System::set_block_number(0);
assert_ok!(Democracy::external_propose(
Origin::signed(2),
set_balance_proposal_hash_and_note(1),
));
assert_ok!(propose_set_balance_and_note(6, 2, 2));
fast_forward_to(2);
// both waiting: external goes first.
assert_eq!(
Democracy::referendum_status(0),
Ok(ReferendumStatus {
end: 4,
proposal_hash: set_balance_proposal_hash_and_note(1),
threshold: VoteThreshold::SuperMajorityApprove,
delay: 2,
tally: Tally { ayes: 0, nays: 0, turnout: 0 },
})
);
// replenish external
assert_ok!(Democracy::external_propose(
Origin::signed(2),
set_balance_proposal_hash_and_note(3),
));
fast_forward_to(4);
// both waiting: public goes next.
assert_eq!(
Democracy::referendum_status(1),
Ok(ReferendumStatus {
end: 6,
proposal_hash: set_balance_proposal_hash_and_note(2),
threshold: VoteThreshold::SuperMajorityApprove,
delay: 2,
tally: Tally { ayes: 0, nays: 0, turnout: 0 },
})
);
// don't replenish public
fast_forward_to(6);
// it's external "turn" again, though since public is empty that doesn't really matter
assert_eq!(
Democracy::referendum_status(2),
Ok(ReferendumStatus {
end: 8,
proposal_hash: set_balance_proposal_hash_and_note(3),
threshold: VoteThreshold::SuperMajorityApprove,
delay: 2,
tally: Tally { ayes: 0, nays: 0, turnout: 0 },
})
);
// replenish external
assert_ok!(Democracy::external_propose(
Origin::signed(2),
set_balance_proposal_hash_and_note(5),
));
fast_forward_to(8);
// external goes again because there's no public waiting.
assert_eq!(
Democracy::referendum_status(3),
Ok(ReferendumStatus {
end: 10,
proposal_hash: set_balance_proposal_hash_and_note(5),
threshold: VoteThreshold::SuperMajorityApprove,
delay: 2,
tally: Tally { ayes: 0, nays: 0, turnout: 0 },
})
);
// replenish both
assert_ok!(Democracy::external_propose(
Origin::signed(2),
set_balance_proposal_hash_and_note(7),
));
assert_ok!(propose_set_balance_and_note(6, 4, 2));
fast_forward_to(10);
// public goes now since external went last time.
assert_eq!(
Democracy::referendum_status(4),
Ok(ReferendumStatus {
end: 12,
proposal_hash: set_balance_proposal_hash_and_note(4),
threshold: VoteThreshold::SuperMajorityApprove,
delay: 2,
tally: Tally { ayes: 0, nays: 0, turnout: 0 },
})
);
// replenish public again
assert_ok!(propose_set_balance_and_note(6, 6, 2));
// cancel external
let h = set_balance_proposal_hash_and_note(7);
assert_ok!(Democracy::veto_external(Origin::signed(3), h));
fast_forward_to(12);
// public goes again now since there's no external waiting.
assert_eq!(
Democracy::referendum_status(5),
Ok(ReferendumStatus {
end: 14,
proposal_hash: set_balance_proposal_hash_and_note(6),
threshold: VoteThreshold::SuperMajorityApprove,
delay: 2,
tally: Tally { ayes: 0, nays: 0, turnout: 0 },
})
);
});
}
@@ -0,0 +1,88 @@
// Copyright 2017-2020 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! The tests for fast-tracking functionality.
use super::*;
#[test]
fn fast_track_referendum_works() {
new_test_ext().execute_with(|| {
System::set_block_number(0);
let h = set_balance_proposal_hash_and_note(2);
assert_noop!(Democracy::fast_track(Origin::signed(5), h, 3, 2), Error::<Test>::ProposalMissing);
assert_ok!(Democracy::external_propose_majority(
Origin::signed(3),
set_balance_proposal_hash_and_note(2)
));
assert_noop!(Democracy::fast_track(Origin::signed(1), h, 3, 2), BadOrigin);
assert_ok!(Democracy::fast_track(Origin::signed(5), h, 2, 0));
assert_eq!(
Democracy::referendum_status(0),
Ok(ReferendumStatus {
end: 2,
proposal_hash: set_balance_proposal_hash_and_note(2),
threshold: VoteThreshold::SimpleMajority,
delay: 0,
tally: Tally { ayes: 0, nays: 0, turnout: 0 },
})
);
});
}
#[test]
fn instant_referendum_works() {
new_test_ext().execute_with(|| {
System::set_block_number(0);
let h = set_balance_proposal_hash_and_note(2);
assert_noop!(Democracy::fast_track(Origin::signed(5), h, 3, 2), Error::<Test>::ProposalMissing);
assert_ok!(Democracy::external_propose_majority(
Origin::signed(3),
set_balance_proposal_hash_and_note(2)
));
assert_noop!(Democracy::fast_track(Origin::signed(1), h, 3, 2), BadOrigin);
assert_noop!(Democracy::fast_track(Origin::signed(5), h, 1, 0), BadOrigin);
assert_noop!(Democracy::fast_track(Origin::signed(6), h, 1, 0), Error::<Test>::InstantNotAllowed);
INSTANT_ALLOWED.with(|v| *v.borrow_mut() = true);
assert_ok!(Democracy::fast_track(Origin::signed(6), h, 1, 0));
assert_eq!(
Democracy::referendum_status(0),
Ok(ReferendumStatus {
end: 1,
proposal_hash: set_balance_proposal_hash_and_note(2),
threshold: VoteThreshold::SimpleMajority,
delay: 0,
tally: Tally { ayes: 0, nays: 0, turnout: 0 },
})
);
});
}
#[test]
fn fast_track_referendum_fails_when_no_simple_majority() {
new_test_ext().execute_with(|| {
System::set_block_number(0);
let h = set_balance_proposal_hash_and_note(2);
assert_ok!(Democracy::external_propose(
Origin::signed(2),
set_balance_proposal_hash_and_note(2)
));
assert_noop!(
Democracy::fast_track(Origin::signed(5), h, 3, 2),
Error::<Test>::NotSimpleMajority
);
});
}
@@ -0,0 +1,364 @@
// Copyright 2017-2020 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! The tests for functionality concerning locking and lock-voting.
use super::*;
use std::convert::TryFrom;
fn aye(x: u8, balance: u64) -> AccountVote<u64> {
AccountVote::Standard {
vote: Vote { aye: true, conviction: Conviction::try_from(x).unwrap() },
balance
}
}
fn nay(x: u8, balance: u64) -> AccountVote<u64> {
AccountVote::Standard {
vote: Vote { aye: false, conviction: Conviction::try_from(x).unwrap() },
balance
}
}
fn the_lock(amount: u64) -> BalanceLock<u64> {
BalanceLock {
id: DEMOCRACY_ID,
amount,
reasons: pallet_balances::Reasons::Misc,
}
}
#[test]
fn lock_voting_should_work() {
new_test_ext().execute_with(|| {
System::set_block_number(0);
let r = Democracy::inject_referendum(
2,
set_balance_proposal_hash_and_note(2),
VoteThreshold::SuperMajorityApprove,
0
);
assert_ok!(Democracy::vote(Origin::signed(1), r, nay(5, 10)));
assert_ok!(Democracy::vote(Origin::signed(2), r, aye(4, 20)));
assert_ok!(Democracy::vote(Origin::signed(3), r, aye(3, 30)));
assert_ok!(Democracy::vote(Origin::signed(4), r, aye(2, 40)));
assert_ok!(Democracy::vote(Origin::signed(5), r, nay(1, 50)));
assert_eq!(tally(r), Tally { ayes: 250, nays: 100, turnout: 150 });
// All balances are currently locked.
for i in 1..=5 {
assert_eq!(Balances::locks(i), vec![the_lock(i * 10)]);
}
fast_forward_to(2);
// Referendum passed; 1 and 5 didn't get their way and can now reap and unlock.
assert_ok!(Democracy::remove_vote(Origin::signed(1), r));
assert_ok!(Democracy::unlock(Origin::signed(1), 1));
// Anyone can reap and unlock anyone else's in this context.
assert_ok!(Democracy::remove_other_vote(Origin::signed(2), 5, r));
assert_ok!(Democracy::unlock(Origin::signed(2), 5));
// 2, 3, 4 got their way with the vote, so they cannot be reaped by others.
assert_noop!(Democracy::remove_other_vote(Origin::signed(1), 2, r), Error::<Test>::NoPermission);
// However, they can be unvoted by the owner, though it will make no difference to the lock.
assert_ok!(Democracy::remove_vote(Origin::signed(2), r));
assert_ok!(Democracy::unlock(Origin::signed(2), 2));
assert_eq!(Balances::locks(1), vec![]);
assert_eq!(Balances::locks(2), vec![the_lock(20)]);
assert_eq!(Balances::locks(3), vec![the_lock(30)]);
assert_eq!(Balances::locks(4), vec![the_lock(40)]);
assert_eq!(Balances::locks(5), vec![]);
assert_eq!(Balances::free_balance(42), 2);
fast_forward_to(5);
// No change yet...
assert_noop!(Democracy::remove_other_vote(Origin::signed(1), 4, r), Error::<Test>::NoPermission);
assert_ok!(Democracy::unlock(Origin::signed(1), 4));
assert_eq!(Balances::locks(4), vec![the_lock(40)]);
fast_forward_to(6);
// 4 should now be able to reap and unlock
assert_ok!(Democracy::remove_other_vote(Origin::signed(1), 4, r));
assert_ok!(Democracy::unlock(Origin::signed(1), 4));
assert_eq!(Balances::locks(4), vec![]);
fast_forward_to(9);
assert_noop!(Democracy::remove_other_vote(Origin::signed(1), 3, r), Error::<Test>::NoPermission);
assert_ok!(Democracy::unlock(Origin::signed(1), 3));
assert_eq!(Balances::locks(3), vec![the_lock(30)]);
fast_forward_to(10);
assert_ok!(Democracy::remove_other_vote(Origin::signed(1), 3, r));
assert_ok!(Democracy::unlock(Origin::signed(1), 3));
assert_eq!(Balances::locks(3), vec![]);
// 2 doesn't need to reap_vote here because it was already done before.
fast_forward_to(17);
assert_ok!(Democracy::unlock(Origin::signed(1), 2));
assert_eq!(Balances::locks(2), vec![the_lock(20)]);
fast_forward_to(18);
assert_ok!(Democracy::unlock(Origin::signed(1), 2));
assert_eq!(Balances::locks(2), vec![]);
});
}
#[test]
fn no_locks_without_conviction_should_work() {
new_test_ext().execute_with(|| {
System::set_block_number(0);
let r = Democracy::inject_referendum(
2,
set_balance_proposal_hash_and_note(2),
VoteThreshold::SuperMajorityApprove,
0,
);
assert_ok!(Democracy::vote(Origin::signed(1), r, aye(0, 10)));
fast_forward_to(2);
assert_eq!(Balances::free_balance(42), 2);
assert_ok!(Democracy::remove_other_vote(Origin::signed(2), 1, r));
assert_ok!(Democracy::unlock(Origin::signed(2), 1));
assert_eq!(Balances::locks(1), vec![]);
});
}
#[test]
fn lock_voting_should_work_with_delegation() {
new_test_ext().execute_with(|| {
System::set_block_number(1);
let r = Democracy::inject_referendum(
2,
set_balance_proposal_hash_and_note(2),
VoteThreshold::SuperMajorityApprove,
0
);
assert_ok!(Democracy::vote(Origin::signed(1), r, nay(5, 10)));
assert_ok!(Democracy::vote(Origin::signed(2), r, aye(4, 20)));
assert_ok!(Democracy::vote(Origin::signed(3), r, aye(3, 30)));
assert_ok!(Democracy::delegate(Origin::signed(4), 2, Conviction::Locked2x, 40));
assert_ok!(Democracy::vote(Origin::signed(5), r, nay(1, 50)));
assert_eq!(tally(r), Tally { ayes: 250, nays: 100, turnout: 150 });
next_block();
next_block();
assert_eq!(Balances::free_balance(42), 2);
});
}
fn setup_three_referenda() -> (u32, u32, u32) {
System::set_block_number(0);
let r1 = Democracy::inject_referendum(
2,
set_balance_proposal_hash_and_note(2),
VoteThreshold::SimpleMajority,
0
);
assert_ok!(Democracy::vote(Origin::signed(5), r1, aye(4, 10)));
let r2 = Democracy::inject_referendum(
2,
set_balance_proposal_hash_and_note(2),
VoteThreshold::SimpleMajority,
0
);
assert_ok!(Democracy::vote(Origin::signed(5), r2, aye(3, 20)));
let r3 = Democracy::inject_referendum(
2,
set_balance_proposal_hash_and_note(2),
VoteThreshold::SimpleMajority,
0
);
assert_ok!(Democracy::vote(Origin::signed(5), r3, aye(2, 50)));
fast_forward_to(2);
(r1, r2, r3)
}
#[test]
fn prior_lockvotes_should_be_enforced() {
new_test_ext().execute_with(|| {
let r = setup_three_referenda();
// r.0 locked 10 until #18.
// r.1 locked 20 until #10.
// r.2 locked 50 until #6.
fast_forward_to(5);
assert_noop!(Democracy::remove_other_vote(Origin::signed(1), 5, r.2), Error::<Test>::NoPermission);
assert_ok!(Democracy::unlock(Origin::signed(5), 5));
assert_eq!(Balances::locks(5), vec![the_lock(50)]);
fast_forward_to(6);
assert_ok!(Democracy::remove_other_vote(Origin::signed(1), 5, r.2));
assert_ok!(Democracy::unlock(Origin::signed(5), 5));
assert_eq!(Balances::locks(5), vec![the_lock(20)]);
fast_forward_to(9);
assert_noop!(Democracy::remove_other_vote(Origin::signed(1), 5, r.1), Error::<Test>::NoPermission);
assert_ok!(Democracy::unlock(Origin::signed(5), 5));
assert_eq!(Balances::locks(5), vec![the_lock(20)]);
fast_forward_to(10);
assert_ok!(Democracy::remove_other_vote(Origin::signed(1), 5, r.1));
assert_ok!(Democracy::unlock(Origin::signed(5), 5));
assert_eq!(Balances::locks(5), vec![the_lock(10)]);
fast_forward_to(17);
assert_noop!(Democracy::remove_other_vote(Origin::signed(1), 5, r.0), Error::<Test>::NoPermission);
assert_ok!(Democracy::unlock(Origin::signed(5), 5));
assert_eq!(Balances::locks(5), vec![the_lock(10)]);
fast_forward_to(18);
assert_ok!(Democracy::remove_other_vote(Origin::signed(1), 5, r.0));
assert_ok!(Democracy::unlock(Origin::signed(5), 5));
assert_eq!(Balances::locks(5), vec![]);
});
}
#[test]
fn single_consolidation_of_lockvotes_should_work_as_before() {
new_test_ext().execute_with(|| {
let r = setup_three_referenda();
// r.0 locked 10 until #18.
// r.1 locked 20 until #10.
// r.2 locked 50 until #6.
fast_forward_to(5);
assert_ok!(Democracy::remove_vote(Origin::signed(5), r.2));
assert_ok!(Democracy::unlock(Origin::signed(5), 5));
assert_eq!(Balances::locks(5), vec![the_lock(50)]);
fast_forward_to(6);
assert_ok!(Democracy::unlock(Origin::signed(5), 5));
assert_eq!(Balances::locks(5), vec![the_lock(20)]);
fast_forward_to(9);
assert_ok!(Democracy::remove_vote(Origin::signed(5), r.1));
assert_ok!(Democracy::unlock(Origin::signed(5), 5));
assert_eq!(Balances::locks(5), vec![the_lock(20)]);
fast_forward_to(10);
assert_ok!(Democracy::unlock(Origin::signed(5), 5));
assert_eq!(Balances::locks(5), vec![the_lock(10)]);
fast_forward_to(17);
assert_ok!(Democracy::remove_vote(Origin::signed(5), r.0));
assert_ok!(Democracy::unlock(Origin::signed(5), 5));
assert_eq!(Balances::locks(5), vec![the_lock(10)]);
fast_forward_to(18);
assert_ok!(Democracy::unlock(Origin::signed(5), 5));
assert_eq!(Balances::locks(5), vec![]);
});
}
#[test]
fn multi_consolidation_of_lockvotes_should_be_conservative() {
new_test_ext().execute_with(|| {
let r = setup_three_referenda();
// r.0 locked 10 until #18.
// r.1 locked 20 until #10.
// r.2 locked 50 until #6.
assert_ok!(Democracy::remove_vote(Origin::signed(5), r.2));
assert_ok!(Democracy::remove_vote(Origin::signed(5), r.1));
assert_ok!(Democracy::remove_vote(Origin::signed(5), r.0));
fast_forward_to(6);
assert_ok!(Democracy::unlock(Origin::signed(5), 5));
assert!(Balances::locks(5)[0].amount >= 20);
fast_forward_to(10);
assert_ok!(Democracy::unlock(Origin::signed(5), 5));
assert!(Balances::locks(5)[0].amount >= 10);
fast_forward_to(18);
assert_ok!(Democracy::unlock(Origin::signed(5), 5));
assert_eq!(Balances::locks(5), vec![]);
});
}
#[test]
fn locks_should_persist_from_voting_to_delegation() {
new_test_ext().execute_with(|| {
System::set_block_number(0);
let r = Democracy::inject_referendum(
2,
set_balance_proposal_hash_and_note(2),
VoteThreshold::SimpleMajority,
0
);
assert_ok!(Democracy::vote(Origin::signed(5), r, aye(4, 10)));
fast_forward_to(2);
assert_ok!(Democracy::remove_vote(Origin::signed(5), r));
// locked 10 until #18.
assert_ok!(Democracy::delegate(Origin::signed(5), 1, Conviction::Locked3x, 20));
// locked 20.
assert!(Balances::locks(5)[0].amount == 20);
assert_ok!(Democracy::undelegate(Origin::signed(5)));
// locked 20 until #10
fast_forward_to(9);
assert_ok!(Democracy::unlock(Origin::signed(5), 5));
assert!(Balances::locks(5)[0].amount == 20);
fast_forward_to(10);
assert_ok!(Democracy::unlock(Origin::signed(5), 5));
assert!(Balances::locks(5)[0].amount >= 10);
fast_forward_to(17);
assert_ok!(Democracy::unlock(Origin::signed(5), 5));
assert!(Balances::locks(5)[0].amount >= 10);
fast_forward_to(18);
assert_ok!(Democracy::unlock(Origin::signed(5), 5));
assert_eq!(Balances::locks(5), vec![]);
});
}
#[test]
fn locks_should_persist_from_delegation_to_voting() {
new_test_ext().execute_with(|| {
System::set_block_number(0);
assert_ok!(Democracy::delegate(Origin::signed(5), 1, Conviction::Locked5x, 5));
assert_ok!(Democracy::undelegate(Origin::signed(5)));
// locked 5 until #32
let r = setup_three_referenda();
// r.0 locked 10 until #18.
// r.1 locked 20 until #10.
// r.2 locked 50 until #6.
assert_ok!(Democracy::remove_vote(Origin::signed(5), r.2));
assert_ok!(Democracy::remove_vote(Origin::signed(5), r.1));
assert_ok!(Democracy::remove_vote(Origin::signed(5), r.0));
fast_forward_to(6);
assert_ok!(Democracy::unlock(Origin::signed(5), 5));
assert!(Balances::locks(5)[0].amount >= 20);
fast_forward_to(10);
assert_ok!(Democracy::unlock(Origin::signed(5), 5));
assert!(Balances::locks(5)[0].amount >= 10);
fast_forward_to(18);
assert_ok!(Democracy::unlock(Origin::signed(5), 5));
assert!(Balances::locks(5)[0].amount >= 5);
fast_forward_to(32);
assert_ok!(Democracy::unlock(Origin::signed(5), 5));
assert_eq!(Balances::locks(5), vec![]);
});
}
@@ -0,0 +1,164 @@
// Copyright 2017-2020 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! The preimage tests.
use super::*;
#[test]
fn missing_preimage_should_fail() {
new_test_ext().execute_with(|| {
System::set_block_number(1);
let r = Democracy::inject_referendum(
2,
set_balance_proposal_hash(2),
VoteThreshold::SuperMajorityApprove,
0
);
assert_ok!(Democracy::vote(Origin::signed(1), r, aye(1)));
next_block();
next_block();
assert_eq!(Balances::free_balance(42), 0);
});
}
#[test]
fn preimage_deposit_should_be_required_and_returned() {
new_test_ext().execute_with(|| {
System::set_block_number(1);
// fee of 100 is too much.
PREIMAGE_BYTE_DEPOSIT.with(|v| *v.borrow_mut() = 100);
assert_noop!(
Democracy::note_preimage(Origin::signed(6), vec![0; 500]),
BalancesError::<Test, _>::InsufficientBalance,
);
// fee of 1 is reasonable.
PREIMAGE_BYTE_DEPOSIT.with(|v| *v.borrow_mut() = 1);
let r = Democracy::inject_referendum(
2,
set_balance_proposal_hash_and_note(2),
VoteThreshold::SuperMajorityApprove,
0
);
assert_ok!(Democracy::vote(Origin::signed(1), r, aye(1)));
assert_eq!(Balances::reserved_balance(6), 12);
next_block();
next_block();
assert_eq!(Balances::reserved_balance(6), 0);
assert_eq!(Balances::free_balance(6), 60);
assert_eq!(Balances::free_balance(42), 2);
});
}
#[test]
fn preimage_deposit_should_be_reapable_earlier_by_owner() {
new_test_ext().execute_with(|| {
System::set_block_number(1);
PREIMAGE_BYTE_DEPOSIT.with(|v| *v.borrow_mut() = 1);
assert_ok!(Democracy::note_preimage(Origin::signed(6), set_balance_proposal(2)));
assert_eq!(Balances::reserved_balance(6), 12);
next_block();
assert_noop!(
Democracy::reap_preimage(Origin::signed(6), set_balance_proposal_hash(2)),
Error::<Test>::TooEarly
);
next_block();
assert_ok!(Democracy::reap_preimage(Origin::signed(6), set_balance_proposal_hash(2)));
assert_eq!(Balances::free_balance(6), 60);
assert_eq!(Balances::reserved_balance(6), 0);
});
}
#[test]
fn preimage_deposit_should_be_reapable() {
new_test_ext().execute_with(|| {
System::set_block_number(1);
assert_noop!(
Democracy::reap_preimage(Origin::signed(5), set_balance_proposal_hash(2)),
Error::<Test>::PreimageMissing
);
PREIMAGE_BYTE_DEPOSIT.with(|v| *v.borrow_mut() = 1);
assert_ok!(Democracy::note_preimage(Origin::signed(6), set_balance_proposal(2)));
assert_eq!(Balances::reserved_balance(6), 12);
next_block();
next_block();
next_block();
assert_noop!(
Democracy::reap_preimage(Origin::signed(5), set_balance_proposal_hash(2)),
Error::<Test>::TooEarly
);
next_block();
assert_ok!(Democracy::reap_preimage(Origin::signed(5), set_balance_proposal_hash(2)));
assert_eq!(Balances::reserved_balance(6), 0);
assert_eq!(Balances::free_balance(6), 48);
assert_eq!(Balances::free_balance(5), 62);
});
}
#[test]
fn noting_imminent_preimage_for_free_should_work() {
new_test_ext().execute_with(|| {
System::set_block_number(1);
PREIMAGE_BYTE_DEPOSIT.with(|v| *v.borrow_mut() = 1);
let r = Democracy::inject_referendum(
2,
set_balance_proposal_hash(2),
VoteThreshold::SuperMajorityApprove,
1
);
assert_ok!(Democracy::vote(Origin::signed(1), r, aye(1)));
assert_noop!(
Democracy::note_imminent_preimage(Origin::signed(7), set_balance_proposal(2)),
Error::<Test>::NotImminent
);
next_block();
// Now we're in the dispatch queue it's all good.
assert_ok!(Democracy::note_imminent_preimage(Origin::signed(7), set_balance_proposal(2)));
next_block();
assert_eq!(Balances::free_balance(42), 2);
});
}
#[test]
fn reaping_imminent_preimage_should_fail() {
new_test_ext().execute_with(|| {
System::set_block_number(1);
let h = set_balance_proposal_hash_and_note(2);
let r = Democracy::inject_referendum(3, h, VoteThreshold::SuperMajorityApprove, 1);
assert_ok!(Democracy::vote(Origin::signed(1), r, aye(1)));
next_block();
next_block();
// now imminent.
assert_noop!(Democracy::reap_preimage(Origin::signed(6), h), Error::<Test>::Imminent);
});
}
@@ -0,0 +1,104 @@
// Copyright 2017-2020 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! The tests for functionality concerning proxying.
use super::*;
#[test]
fn proxy_should_work() {
new_test_ext().execute_with(|| {
assert_eq!(Democracy::proxy(10), None);
assert!(System::allow_death(&10));
assert_noop!(Democracy::activate_proxy(Origin::signed(1), 10), Error::<Test>::NotOpen);
assert_ok!(Democracy::open_proxy(Origin::signed(10), 1));
assert!(!System::allow_death(&10));
assert_eq!(Democracy::proxy(10), Some(ProxyState::Open(1)));
assert_noop!(Democracy::activate_proxy(Origin::signed(2), 10), Error::<Test>::WrongOpen);
assert_ok!(Democracy::activate_proxy(Origin::signed(1), 10));
assert_eq!(Democracy::proxy(10), Some(ProxyState::Active(1)));
// Can't set when already set.
assert_noop!(Democracy::activate_proxy(Origin::signed(2), 10), Error::<Test>::AlreadyProxy);
// But this works because 11 isn't proxying.
assert_ok!(Democracy::open_proxy(Origin::signed(11), 2));
assert_ok!(Democracy::activate_proxy(Origin::signed(2), 11));
assert_eq!(Democracy::proxy(10), Some(ProxyState::Active(1)));
assert_eq!(Democracy::proxy(11), Some(ProxyState::Active(2)));
// 2 cannot fire 1's proxy:
assert_noop!(Democracy::deactivate_proxy(Origin::signed(2), 10), Error::<Test>::WrongProxy);
// 1 deactivates their proxy:
assert_ok!(Democracy::deactivate_proxy(Origin::signed(1), 10));
assert_eq!(Democracy::proxy(10), Some(ProxyState::Open(1)));
// but the proxy account cannot be killed until the proxy is closed.
assert!(!System::allow_death(&10));
// and then 10 closes it completely:
assert_ok!(Democracy::close_proxy(Origin::signed(10)));
assert_eq!(Democracy::proxy(10), None);
assert!(System::allow_death(&10));
// 11 just closes without 2's "permission".
assert_ok!(Democracy::close_proxy(Origin::signed(11)));
assert_eq!(Democracy::proxy(11), None);
assert!(System::allow_death(&11));
});
}
#[test]
fn voting_and_removing_votes_should_work_with_proxy() {
new_test_ext().execute_with(|| {
System::set_block_number(0);
assert_ok!(propose_set_balance_and_note(1, 2, 1));
fast_forward_to(2);
let r = 0;
assert_ok!(Democracy::open_proxy(Origin::signed(10), 1));
assert_ok!(Democracy::activate_proxy(Origin::signed(1), 10));
assert_ok!(Democracy::proxy_vote(Origin::signed(10), r, aye(1)));
assert_eq!(tally(r), Tally { ayes: 1, nays: 0, turnout: 10 });
assert_ok!(Democracy::proxy_remove_vote(Origin::signed(10), r));
assert_eq!(tally(r), Tally { ayes: 0, nays: 0, turnout: 0 });
});
}
#[test]
fn delegation_and_undelegation_should_work_with_proxy() {
new_test_ext().execute_with(|| {
System::set_block_number(0);
assert_ok!(propose_set_balance_and_note(1, 2, 1));
fast_forward_to(2);
let r = 0;
assert_ok!(Democracy::open_proxy(Origin::signed(10), 1));
assert_ok!(Democracy::activate_proxy(Origin::signed(1), 10));
assert_ok!(Democracy::vote(Origin::signed(2), r, aye(2)));
assert_ok!(Democracy::proxy_delegate(Origin::signed(10), 2, Conviction::None, 10));
assert_eq!(tally(r), Tally { ayes: 3, nays: 0, turnout: 30 });
assert_ok!(Democracy::proxy_undelegate(Origin::signed(10)));
assert_eq!(tally(r), Tally { ayes: 2, nays: 0, turnout: 20 });
});
}
@@ -0,0 +1,104 @@
// Copyright 2017-2020 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! The tests for the public proposal queue.
use super::*;
#[test]
fn backing_for_should_work() {
new_test_ext().execute_with(|| {
System::set_block_number(1);
assert_ok!(propose_set_balance_and_note(1, 2, 2));
assert_ok!(propose_set_balance_and_note(1, 4, 4));
assert_ok!(propose_set_balance_and_note(1, 3, 3));
assert_eq!(Democracy::backing_for(0), Some(2));
assert_eq!(Democracy::backing_for(1), Some(4));
assert_eq!(Democracy::backing_for(2), Some(3));
});
}
#[test]
fn deposit_for_proposals_should_be_taken() {
new_test_ext().execute_with(|| {
System::set_block_number(1);
assert_ok!(propose_set_balance_and_note(1, 2, 5));
assert_ok!(Democracy::second(Origin::signed(2), 0));
assert_ok!(Democracy::second(Origin::signed(5), 0));
assert_ok!(Democracy::second(Origin::signed(5), 0));
assert_ok!(Democracy::second(Origin::signed(5), 0));
assert_eq!(Balances::free_balance(1), 5);
assert_eq!(Balances::free_balance(2), 15);
assert_eq!(Balances::free_balance(5), 35);
});
}
#[test]
fn deposit_for_proposals_should_be_returned() {
new_test_ext().execute_with(|| {
System::set_block_number(1);
assert_ok!(propose_set_balance_and_note(1, 2, 5));
assert_ok!(Democracy::second(Origin::signed(2), 0));
assert_ok!(Democracy::second(Origin::signed(5), 0));
assert_ok!(Democracy::second(Origin::signed(5), 0));
assert_ok!(Democracy::second(Origin::signed(5), 0));
fast_forward_to(3);
assert_eq!(Balances::free_balance(1), 10);
assert_eq!(Balances::free_balance(2), 20);
assert_eq!(Balances::free_balance(5), 50);
});
}
#[test]
fn proposal_with_deposit_below_minimum_should_not_work() {
new_test_ext().execute_with(|| {
System::set_block_number(1);
assert_noop!(propose_set_balance(1, 2, 0), Error::<Test>::ValueLow);
});
}
#[test]
fn poor_proposer_should_not_work() {
new_test_ext().execute_with(|| {
System::set_block_number(1);
assert_noop!(propose_set_balance(1, 2, 11), BalancesError::<Test, _>::InsufficientBalance);
});
}
#[test]
fn poor_seconder_should_not_work() {
new_test_ext().execute_with(|| {
System::set_block_number(1);
assert_ok!(propose_set_balance_and_note(2, 2, 11));
assert_noop!(Democracy::second(Origin::signed(1), 0), BalancesError::<Test, _>::InsufficientBalance);
});
}
#[test]
fn runners_up_should_come_after() {
new_test_ext().execute_with(|| {
System::set_block_number(0);
assert_ok!(propose_set_balance_and_note(1, 2, 2));
assert_ok!(propose_set_balance_and_note(1, 4, 4));
assert_ok!(propose_set_balance_and_note(1, 3, 3));
fast_forward_to(2);
assert_ok!(Democracy::vote(Origin::signed(1), 0, aye(1)));
fast_forward_to(4);
assert_ok!(Democracy::vote(Origin::signed(1), 1, aye(1)));
fast_forward_to(6);
assert_ok!(Democracy::vote(Origin::signed(1), 2, aye(1)));
});
}
@@ -0,0 +1,115 @@
// Copyright 2017-2020 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! The tests for functionality concerning normal starting, ending and enacting of referenda.
use super::*;
#[test]
fn simple_passing_should_work() {
new_test_ext().execute_with(|| {
System::set_block_number(1);
let r = Democracy::inject_referendum(
2,
set_balance_proposal_hash_and_note(2),
VoteThreshold::SuperMajorityApprove,
0
);
assert_ok!(Democracy::vote(Origin::signed(1), r, aye(1)));
assert_eq!(tally(r), Tally { ayes: 1, nays: 0, turnout: 10 });
next_block();
next_block();
assert_eq!(Balances::free_balance(42), 2);
});
}
#[test]
fn simple_failing_should_work() {
new_test_ext().execute_with(|| {
System::set_block_number(1);
let r = Democracy::inject_referendum(
2,
set_balance_proposal_hash_and_note(2),
VoteThreshold::SuperMajorityApprove,
0
);
assert_ok!(Democracy::vote(Origin::signed(1), r, nay(1)));
assert_eq!(tally(r), Tally { ayes: 0, nays: 1, turnout: 10 });
next_block();
next_block();
assert_eq!(Balances::free_balance(42), 0);
});
}
#[test]
fn ooo_inject_referendums_should_work() {
new_test_ext().execute_with(|| {
System::set_block_number(1);
let r1 = Democracy::inject_referendum(
3,
set_balance_proposal_hash_and_note(3),
VoteThreshold::SuperMajorityApprove,
0
);
let r2 = Democracy::inject_referendum(
2,
set_balance_proposal_hash_and_note(2),
VoteThreshold::SuperMajorityApprove,
0
);
assert_ok!(Democracy::vote(Origin::signed(1), r2, aye(1)));
assert_eq!(tally(r2), Tally { ayes: 1, nays: 0, turnout: 10 });
next_block();
assert_eq!(Balances::free_balance(42), 2);
assert_ok!(Democracy::vote(Origin::signed(1), r1, aye(1)));
assert_eq!(tally(r1), Tally { ayes: 1, nays: 0, turnout: 10 });
next_block();
assert_eq!(Balances::free_balance(42), 3);
});
}
#[test]
fn delayed_enactment_should_work() {
new_test_ext().execute_with(|| {
System::set_block_number(1);
let r = Democracy::inject_referendum(
2,
set_balance_proposal_hash_and_note(2),
VoteThreshold::SuperMajorityApprove,
1
);
assert_ok!(Democracy::vote(Origin::signed(1), r, aye(1)));
assert_ok!(Democracy::vote(Origin::signed(2), r, aye(2)));
assert_ok!(Democracy::vote(Origin::signed(3), r, aye(3)));
assert_ok!(Democracy::vote(Origin::signed(4), r, aye(4)));
assert_ok!(Democracy::vote(Origin::signed(5), r, aye(5)));
assert_ok!(Democracy::vote(Origin::signed(6), r, aye(6)));
assert_eq!(tally(r), Tally { ayes: 21, nays: 0, turnout: 210 });
next_block();
assert_eq!(Balances::free_balance(42), 0);
next_block();
assert_eq!(Balances::free_balance(42), 2);
});
}
@@ -0,0 +1,170 @@
// Copyright 2017-2020 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! The tests for normal voting functionality.
use super::*;
#[test]
fn overvoting_should_fail() {
new_test_ext().execute_with(|| {
let r = begin_referendum();
assert_noop!(Democracy::vote(Origin::signed(1), r, aye(2)), Error::<Test>::InsufficientFunds);
});
}
#[test]
fn split_voting_should_work() {
new_test_ext().execute_with(|| {
let r = begin_referendum();
let v = AccountVote::Split { aye: 40, nay: 20 };
assert_noop!(Democracy::vote(Origin::signed(5), r, v), Error::<Test>::InsufficientFunds);
let v = AccountVote::Split { aye: 30, nay: 20 };
assert_ok!(Democracy::vote(Origin::signed(5), r, v));
assert_eq!(tally(r), Tally { ayes: 3, nays: 2, turnout: 50 });
});
}
#[test]
fn split_vote_cancellation_should_work() {
new_test_ext().execute_with(|| {
let r = begin_referendum();
let v = AccountVote::Split { aye: 30, nay: 20 };
assert_ok!(Democracy::vote(Origin::signed(5), r, v));
assert_ok!(Democracy::remove_vote(Origin::signed(5), r));
assert_eq!(tally(r), Tally { ayes: 0, nays: 0, turnout: 0 });
assert_ok!(Democracy::unlock(Origin::signed(5), 5));
assert_eq!(Balances::locks(5), vec![]);
});
}
#[test]
fn single_proposal_should_work() {
new_test_ext().execute_with(|| {
System::set_block_number(0);
assert_ok!(propose_set_balance_and_note(1, 2, 1));
let r = 0;
assert!(Democracy::referendum_info(r).is_none());
// start of 2 => next referendum scheduled.
fast_forward_to(2);
assert_ok!(Democracy::vote(Origin::signed(1), r, aye(1)));
assert_eq!(Democracy::referendum_count(), 1);
assert_eq!(
Democracy::referendum_status(0),
Ok(ReferendumStatus {
end: 4,
proposal_hash: set_balance_proposal_hash_and_note(2),
threshold: VoteThreshold::SuperMajorityApprove,
delay: 2,
tally: Tally { ayes: 1, nays: 0, turnout: 10 },
})
);
fast_forward_to(3);
// referendum still running
assert!(Democracy::referendum_status(0).is_ok());
// referendum runs during 2 and 3, ends @ start of 4.
fast_forward_to(4);
assert!(Democracy::referendum_status(0).is_err());
assert_eq!(Democracy::dispatch_queue(), vec![
(6, set_balance_proposal_hash_and_note(2), 0)
]);
// referendum passes and wait another two blocks for enactment.
fast_forward_to(6);
assert_eq!(Balances::free_balance(42), 2);
});
}
#[test]
fn controversial_voting_should_work() {
new_test_ext().execute_with(|| {
System::set_block_number(1);
let r = Democracy::inject_referendum(
2,
set_balance_proposal_hash_and_note(2),
VoteThreshold::SuperMajorityApprove,
0
);
assert_ok!(Democracy::vote(Origin::signed(1), r, big_aye(1)));
assert_ok!(Democracy::vote(Origin::signed(2), r, big_nay(2)));
assert_ok!(Democracy::vote(Origin::signed(3), r, big_nay(3)));
assert_ok!(Democracy::vote(Origin::signed(4), r, big_aye(4)));
assert_ok!(Democracy::vote(Origin::signed(5), r, big_nay(5)));
assert_ok!(Democracy::vote(Origin::signed(6), r, big_aye(6)));
assert_eq!(tally(r), Tally { ayes: 110, nays: 100, turnout: 210 });
next_block();
next_block();
assert_eq!(Balances::free_balance(42), 2);
});
}
#[test]
fn controversial_low_turnout_voting_should_work() {
new_test_ext().execute_with(|| {
System::set_block_number(1);
let r = Democracy::inject_referendum(
2,
set_balance_proposal_hash_and_note(2),
VoteThreshold::SuperMajorityApprove,
0
);
assert_ok!(Democracy::vote(Origin::signed(5), r, big_nay(5)));
assert_ok!(Democracy::vote(Origin::signed(6), r, big_aye(6)));
assert_eq!(tally(r), Tally { ayes: 60, nays: 50, turnout: 110 });
next_block();
next_block();
assert_eq!(Balances::free_balance(42), 0);
});
}
#[test]
fn passing_low_turnout_voting_should_work() {
new_test_ext().execute_with(|| {
assert_eq!(Balances::free_balance(42), 0);
assert_eq!(Balances::total_issuance(), 210);
System::set_block_number(1);
let r = Democracy::inject_referendum(
2,
set_balance_proposal_hash_and_note(2),
VoteThreshold::SuperMajorityApprove,
0
);
assert_ok!(Democracy::vote(Origin::signed(4), r, big_aye(4)));
assert_ok!(Democracy::vote(Origin::signed(5), r, big_nay(5)));
assert_ok!(Democracy::vote(Origin::signed(6), r, big_aye(6)));
assert_eq!(tally(r), Tally { ayes: 100, nays: 50, turnout: 150 });
next_block();
next_block();
assert_eq!(Balances::free_balance(42), 2);
});
}