Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Prevent events from being emitted during genesis construction (#5463)
Browse files Browse the repository at this point in the history
* Don't populate runtime events in genesis

* typo

* Change to block zero

* Fix vesting tests

* Update frame/system/src/lib.rs

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

* Update frame/system/src/lib.rs

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

* Add test

* Fix test

* Fix contract tests

* Fix phragmen tests

* Fix Generic Assets Tests

* Fix offences tests

* Fix im-online

* fix recovery

* Fix utility tests

* Shorter

* Use ext

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
  • Loading branch information
shawntabrizi and bkchr authored Mar 31, 2020
1 parent 5f51dd8 commit d0bbb22
Show file tree
Hide file tree
Showing 22 changed files with 73 additions and 110 deletions.
3 changes: 1 addition & 2 deletions bin/node/executor/tests/submit_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,8 @@ fn submitted_transaction_should_be_valid() {
priority: 2_411_002_000_000,
requires: vec![],
provides: vec![(address, 0).encode()],
longevity: 127,
longevity: 128,
propagate: true,
});
});
}

19 changes: 4 additions & 15 deletions frame/collective/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,19 +590,20 @@ mod tests {
);

fn make_ext() -> sp_io::TestExternalities {
GenesisConfig {
let mut ext: sp_io::TestExternalities = GenesisConfig {
collective_Instance1: Some(collective::GenesisConfig {
members: vec![1, 2, 3],
phantom: Default::default(),
}),
collective: None,
}.build_storage().unwrap().into()
}.build_storage().unwrap().into();
ext.execute_with(|| System::set_block_number(1));
ext
}

#[test]
fn motions_basic_environment_works() {
make_ext().execute_with(|| {
System::set_block_number(1);
assert_eq!(Collective::members(), vec![1, 2, 3]);
assert_eq!(Collective::proposals(), Vec::<H256>::new());
});
Expand All @@ -615,7 +616,6 @@ mod tests {
#[test]
fn close_works() {
make_ext().execute_with(|| {
System::set_block_number(1);
let proposal = make_proposal(42);
let hash = BlakeTwo256::hash_of(&proposal);

Expand Down Expand Up @@ -644,7 +644,6 @@ mod tests {
#[test]
fn close_with_prime_works() {
make_ext().execute_with(|| {
System::set_block_number(1);
let proposal = make_proposal(42);
let hash = BlakeTwo256::hash_of(&proposal);
assert_ok!(Collective::set_members(Origin::ROOT, vec![1, 2, 3], Some(3)));
Expand All @@ -668,7 +667,6 @@ mod tests {
#[test]
fn close_with_voting_prime_works() {
make_ext().execute_with(|| {
System::set_block_number(1);
let proposal = make_proposal(42);
let hash = BlakeTwo256::hash_of(&proposal);
assert_ok!(Collective::set_members(Origin::ROOT, vec![1, 2, 3], Some(1)));
Expand All @@ -693,7 +691,6 @@ mod tests {
#[test]
fn removal_of_old_voters_votes_works() {
make_ext().execute_with(|| {
System::set_block_number(1);
let proposal = make_proposal(42);
let hash = BlakeTwo256::hash_of(&proposal);
let end = 4;
Expand Down Expand Up @@ -728,7 +725,6 @@ mod tests {
#[test]
fn removal_of_old_voters_votes_works_with_set_members() {
make_ext().execute_with(|| {
System::set_block_number(1);
let proposal = make_proposal(42);
let hash = BlakeTwo256::hash_of(&proposal);
let end = 4;
Expand Down Expand Up @@ -763,7 +759,6 @@ mod tests {
#[test]
fn propose_works() {
make_ext().execute_with(|| {
System::set_block_number(1);
let proposal = make_proposal(42);
let hash = proposal.blake2_256().into();
let end = 4;
Expand Down Expand Up @@ -793,7 +788,6 @@ mod tests {
#[test]
fn motions_ignoring_non_collective_proposals_works() {
make_ext().execute_with(|| {
System::set_block_number(1);
let proposal = make_proposal(42);
assert_noop!(
Collective::propose(Origin::signed(42), 3, Box::new(proposal.clone())),
Expand All @@ -805,7 +799,6 @@ mod tests {
#[test]
fn motions_ignoring_non_collective_votes_works() {
make_ext().execute_with(|| {
System::set_block_number(1);
let proposal = make_proposal(42);
let hash: H256 = proposal.blake2_256().into();
assert_ok!(Collective::propose(Origin::signed(1), 3, Box::new(proposal.clone())));
Expand Down Expand Up @@ -833,7 +826,6 @@ mod tests {
#[test]
fn motions_revoting_works() {
make_ext().execute_with(|| {
System::set_block_number(1);
let proposal = make_proposal(42);
let hash: H256 = proposal.blake2_256().into();
let end = 4;
Expand Down Expand Up @@ -885,7 +877,6 @@ mod tests {
#[test]
fn motions_reproposing_disapproved_works() {
make_ext().execute_with(|| {
System::set_block_number(1);
let proposal = make_proposal(42);
let hash: H256 = proposal.blake2_256().into();
assert_ok!(Collective::propose(Origin::signed(1), 3, Box::new(proposal.clone())));
Expand All @@ -899,7 +890,6 @@ mod tests {
#[test]
fn motions_disapproval_works() {
make_ext().execute_with(|| {
System::set_block_number(1);
let proposal = make_proposal(42);
let hash: H256 = proposal.blake2_256().into();
assert_ok!(Collective::propose(Origin::signed(1), 3, Box::new(proposal.clone())));
Expand Down Expand Up @@ -942,7 +932,6 @@ mod tests {
#[test]
fn motions_approval_works() {
make_ext().execute_with(|| {
System::set_block_number(1);
let proposal = make_proposal(42);
let hash: H256 = proposal.blake2_256().into();
assert_ok!(Collective::propose(Origin::signed(1), 2, Box::new(proposal.clone())));
Expand Down
4 changes: 3 additions & 1 deletion frame/contracts/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,9 @@ impl ExtBuilder {
},
gas_price: self.gas_price,
}.assimilate_storage(&mut t).unwrap();
sp_io::TestExternalities::new(t)
let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| System::set_block_number(1));
ext
}
}

Expand Down
4 changes: 3 additions & 1 deletion frame/democracy/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ fn new_test_ext() -> sp_io::TestExternalities {
balances: vec![(1, 10), (2, 20), (3, 30), (4, 40), (5, 50), (6, 60)],
}.assimilate_storage(&mut t).unwrap();
GenesisConfig::default().assimilate_storage(&mut t).unwrap();
sp_io::TestExternalities::new(t)
let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| System::set_block_number(1));
ext
}

type System = frame_system::Module<Test>;
Expand Down
1 change: 0 additions & 1 deletion frame/democracy/src/tests/cancellation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ 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),
Expand Down
1 change: 0 additions & 1 deletion frame/democracy/src/tests/lock_voting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ fn no_locks_without_conviction_should_work() {
#[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),
Expand Down
6 changes: 0 additions & 6 deletions frame/democracy/src/tests/preimage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ 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),
Expand All @@ -40,7 +39,6 @@ fn missing_preimage_should_fail() {
#[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!(
Expand Down Expand Up @@ -71,7 +69,6 @@ fn preimage_deposit_should_be_required_and_returned() {
#[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)));

Expand All @@ -93,7 +90,6 @@ fn preimage_deposit_should_be_reapable_earlier_by_owner() {
#[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
Expand Down Expand Up @@ -122,7 +118,6 @@ fn preimage_deposit_should_be_reapable() {
#[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(
Expand Down Expand Up @@ -152,7 +147,6 @@ fn noting_imminent_preimage_for_free_should_work() {
#[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)));
Expand Down
6 changes: 0 additions & 6 deletions frame/democracy/src/tests/public_proposals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ 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));
Expand All @@ -34,7 +33,6 @@ fn backing_for_should_work() {
#[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));
Expand All @@ -49,7 +47,6 @@ fn deposit_for_proposals_should_be_taken() {
#[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));
Expand All @@ -65,23 +62,20 @@ fn deposit_for_proposals_should_be_returned() {
#[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);
});
Expand Down
4 changes: 0 additions & 4 deletions frame/democracy/src/tests/scheduling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ 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),
Expand All @@ -39,7 +38,6 @@ fn simple_passing_should_work() {
#[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),
Expand All @@ -59,7 +57,6 @@ fn simple_failing_should_work() {
#[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),
Expand Down Expand Up @@ -90,7 +87,6 @@ fn ooo_inject_referendums_should_work() {
#[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),
Expand Down
3 changes: 0 additions & 3 deletions frame/democracy/src/tests/voting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ fn single_proposal_should_work() {
#[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),
Expand All @@ -126,7 +125,6 @@ fn controversial_voting_should_work() {
#[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),
Expand All @@ -151,7 +149,6 @@ fn passing_low_turnout_voting_should_work() {
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),
Expand Down
29 changes: 13 additions & 16 deletions frame/elections-phragmen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@ mod tests {
VOTING_BOND.with(|v| *v.borrow_mut() = self.voter_bond);
TERM_DURATION.with(|v| *v.borrow_mut() = self.term_duration);
DESIRED_RUNNERS_UP.with(|v| *v.borrow_mut() = self.desired_runners_up);
GenesisConfig {
let mut ext: sp_io::TestExternalities = GenesisConfig {
pallet_balances: Some(pallet_balances::GenesisConfig::<Test>{
balances: vec![
(1, 10 * self.balance_factor),
Expand All @@ -1051,7 +1051,9 @@ mod tests {
(6, 60 * self.balance_factor)
],
}),
}.build_storage().unwrap().into()
}.build_storage().unwrap().into();
ext.execute_with(|| System::set_block_number(1));
ext
}
}

Expand All @@ -1072,7 +1074,6 @@ mod tests {
#[test]
fn params_should_work() {
ExtBuilder::default().build().execute_with(|| {
System::set_block_number(1);
assert_eq!(Elections::desired_members(), 2);
assert_eq!(Elections::term_duration(), 5);
assert_eq!(Elections::election_rounds(), 0);
Expand All @@ -1096,7 +1097,6 @@ mod tests {
.build()
.execute_with(||
{
System::set_block_number(1);
assert_eq!(Elections::term_duration(), 0);
assert_eq!(Elections::desired_members(), 2);
assert_eq!(Elections::election_rounds(), 0);
Expand Down Expand Up @@ -1537,10 +1537,9 @@ mod tests {
assert_eq!(balances(&5), (45, 5));

assert_ok!(Elections::report_defunct_voter(Origin::signed(5), 3));
assert_eq!(
System::events()[7].event,
Event::elections(RawEvent::VoterReported(3, 5, true))
);
assert!(System::events().iter().any(|event| {
event.event == Event::elections(RawEvent::VoterReported(3, 5, true))
}));

assert_eq!(balances(&3), (28, 0));
assert_eq!(balances(&5), (47, 5));
Expand All @@ -1566,10 +1565,9 @@ mod tests {
assert_eq!(balances(&5), (45, 5));

assert_ok!(Elections::report_defunct_voter(Origin::signed(5), 4));
assert_eq!(
System::events()[7].event,
Event::elections(RawEvent::VoterReported(4, 5, false))
);
assert!(System::events().iter().any(|event| {
event.event == Event::elections(RawEvent::VoterReported(4, 5, false))
}));

assert_eq!(balances(&4), (35, 5));
assert_eq!(balances(&5), (45, 3));
Expand Down Expand Up @@ -1977,10 +1975,9 @@ mod tests {
// 5 is an outgoing loser. will also get slashed.
assert_eq!(balances(&5), (45, 2));

assert_eq!(
System::events()[6].event,
Event::elections(RawEvent::NewTerm(vec![(4, 40), (5, 50)])),
);
assert!(System::events().iter().any(|event| {
event.event == Event::elections(RawEvent::NewTerm(vec![(4, 40), (5, 50)]))
}));
})
}

Expand Down
Loading

0 comments on commit d0bbb22

Please sign in to comment.