Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
});
});
}

7 changes: 7 additions & 0 deletions frame/contracts/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ fn instantiate_and_call_and_deposit_event() {
let (wasm, code_hash) = compile_module::<Test>(CODE_RETURN_FROM_START_FN).unwrap();

ExtBuilder::default().existential_deposit(100).build().execute_with(|| {
System::set_block_number(1);
Comment thread
shawntabrizi marked this conversation as resolved.
Outdated
Balances::deposit_creating(&ALICE, 1_000_000);

assert_ok!(Contracts::put_code(Origin::signed(ALICE), 100_000, wasm));
Expand Down Expand Up @@ -505,6 +506,7 @@ fn dispatch_call() {
let (wasm, code_hash) = compile_module::<Test>(CODE_DISPATCH_CALL).unwrap();

ExtBuilder::default().existential_deposit(50).build().execute_with(|| {
System::set_block_number(1);
Balances::deposit_creating(&ALICE, 1_000_000);

assert_ok!(Contracts::put_code(Origin::signed(ALICE), 100_000, wasm));
Expand Down Expand Up @@ -643,6 +645,7 @@ fn dispatch_call_not_dispatched_after_top_level_transaction_failure() {
let (wasm, code_hash) = compile_module::<Test>(CODE_DISPATCH_CALL_THEN_TRAP).unwrap();

ExtBuilder::default().existential_deposit(50).build().execute_with(|| {
System::set_block_number(1);
Balances::deposit_creating(&ALICE, 1_000_000);

assert_ok!(Contracts::put_code(Origin::signed(ALICE), 100_000, wasm));
Expand Down Expand Up @@ -899,6 +902,7 @@ fn test_set_rent_code_and_hash() {
let (wasm, code_hash) = compile_module::<Test>(CODE_SET_RENT).unwrap();

ExtBuilder::default().existential_deposit(50).build().execute_with(|| {
System::set_block_number(1);
Balances::deposit_creating(&ALICE, 1_000_000);
assert_ok!(Contracts::put_code(Origin::signed(ALICE), 100_000, wasm));

Expand Down Expand Up @@ -967,6 +971,7 @@ fn deduct_blocks() {
let (wasm, code_hash) = compile_module::<Test>(CODE_SET_RENT).unwrap();

ExtBuilder::default().existential_deposit(50).build().execute_with(|| {
System::set_block_number(1);
// Create
Balances::deposit_creating(&ALICE, 1_000_000);
assert_ok!(Contracts::put_code(Origin::signed(ALICE), 100_000, wasm));
Expand Down Expand Up @@ -1061,6 +1066,7 @@ fn claim_surcharge(blocks: u64, trigger_call: impl Fn() -> bool, removes: bool)
let (wasm, code_hash) = compile_module::<Test>(CODE_SET_RENT).unwrap();

ExtBuilder::default().existential_deposit(50).build().execute_with(|| {
System::set_block_number(1);
// Create
Balances::deposit_creating(&ALICE, 1_000_000);
assert_ok!(Contracts::put_code(Origin::signed(ALICE), 100_000, wasm));
Expand Down Expand Up @@ -1414,6 +1420,7 @@ fn restoration(test_different_storage: bool, test_restore_to_with_dirty_storage:
compile_module::<Test>(CODE_RESTORATION).unwrap();

ExtBuilder::default().existential_deposit(50).build().execute_with(|| {
System::set_block_number(1);
Balances::deposit_creating(&ALICE, 1_000_000);
assert_ok!(Contracts::put_code(Origin::signed(ALICE), 100_000, restoration_wasm));
assert_ok!(Contracts::put_code(Origin::signed(ALICE), 100_000, set_rent_wasm));
Expand Down
21 changes: 9 additions & 12 deletions frame/elections-phragmen/src/lib.rs
Original file line number Diff line number Diff line change
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
3 changes: 3 additions & 0 deletions frame/generic-asset/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,7 @@ fn update_permission_should_raise_event() {
.free_balance((staking_asset_id, origin, initial_balance))
.build()
.execute_with(|| {
System::set_block_number(1);
assert_ok!(GenericAsset::create(
Origin::signed(origin),
AssetOptions {
Expand Down Expand Up @@ -1157,6 +1158,7 @@ fn mint_should_raise_event() {
.free_balance((staking_asset_id, origin, initial_balance))
.build()
.execute_with(|| {
System::set_block_number(1);
assert_ok!(GenericAsset::create(
Origin::signed(origin),
AssetOptions {
Expand Down Expand Up @@ -1195,6 +1197,7 @@ fn burn_should_raise_event() {
.free_balance((staking_asset_id, origin, initial_balance))
.build()
.execute_with(|| {
System::set_block_number(1);
assert_ok!(GenericAsset::create(
Origin::signed(origin),
AssetOptions {
Expand Down
8 changes: 6 additions & 2 deletions frame/im-online/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::{Module, Trait};
use sp_runtime::Perbill;
use sp_staking::{SessionIndex, offence::{ReportOffence, OffenceError}};
use sp_runtime::testing::{Header, UintAuthorityId, TestXt};
use sp_runtime::traits::{IdentityLookup, BlakeTwo256, ConvertInto};
use sp_runtime::traits::{IdentityLookup, BlakeTwo256, ConvertInto, Zero};
use sp_core::H256;
use frame_support::{impl_outer_origin, impl_outer_dispatch, parameter_types, weights::Weight};

Expand Down Expand Up @@ -175,7 +175,11 @@ pub type System = frame_system::Module<Runtime>;
pub type Session = pallet_session::Module<Runtime>;

pub fn advance_session() {
let now = System::block_number();
let now = if System::block_number().is_zero() {
Comment thread
shawntabrizi marked this conversation as resolved.
Outdated
1
} else {
System::block_number()
};
System::set_block_number(now + 1);
Session::rotate_session();
assert_eq!(Session::current_index(), (now / Period::get()) as u32);
Expand Down
3 changes: 3 additions & 0 deletions frame/offences/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ fn should_report_in_different_time_slot() {
#[test]
fn should_deposit_event() {
new_test_ext().execute_with(|| {
System::set_block_number(1);
// given
let time_slot = 42;
assert_eq!(offence_reports(KIND, time_slot), vec![]);
Expand Down Expand Up @@ -141,6 +142,7 @@ fn should_deposit_event() {
#[test]
fn doesnt_deposit_event_for_dups() {
new_test_ext().execute_with(|| {
System::set_block_number(1);
// given
let time_slot = 42;
assert_eq!(offence_reports(KIND, time_slot), vec![]);
Expand Down Expand Up @@ -217,6 +219,7 @@ fn should_properly_count_offences() {
#[test]
fn should_queue_and_resubmit_rejected_offence() {
new_test_ext().execute_with(|| {
System::set_block_number(1);
set_can_report(false);

// will get deferred
Expand Down
24 changes: 21 additions & 3 deletions frame/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ decl_storage! {
ExtrinsicData get(fn extrinsic_data): map hasher(twox_64_concat) u32 => Vec<u8>;

/// The current block number being processed. Set by `execute_block`.
Number get(fn block_number) build(|_| 1.into()): T::BlockNumber;
Number get(fn block_number): T::BlockNumber;

/// Hash of the previous block.
ParentHash get(fn parent_hash) build(|_| hash69()): T::Hash;
Expand Down Expand Up @@ -749,6 +749,10 @@ impl<T: Trait> Module<T> {
/// This will update storage entries that correspond to the specified topics.
/// It is expected that light-clients could subscribe to this topics.
pub fn deposit_event_indexed(topics: &[T::Hash], event: T::Event) {
let block_number = Self::block_number();
// Don't populate events on genesis.
if block_number.is_zero() { return }

let phase = ExecutionPhase::get().unwrap_or_default();
let event = EventRecord {
phase,
Expand Down Expand Up @@ -781,10 +785,9 @@ impl<T: Trait> Module<T> {
return;
}

let block_no = Self::block_number();
for topic in topics {
// The same applies here.
if <EventTopics<T>>::append(topic, &[(block_no, event_idx)]).is_err() {
if <EventTopics<T>>::append(topic, &[(block_number, event_idx)]).is_err() {
return;
}
}
Expand Down Expand Up @@ -2041,6 +2044,7 @@ mod tests {
let mut ext = new_test_ext();
ext.register_extension(sp_core::traits::CallInWasmExt::new(executor));
ext.execute_with(|| {
System::set_block_number(1);
System::set_code(
RawOrigin::Root.into(),
substrate_test_runtime_client::runtime::WASM_BINARY.to_vec(),
Expand Down Expand Up @@ -2068,4 +2072,18 @@ mod tests {
).unwrap();
});
}

#[test]
fn events_not_emitted_during_genesis() {
new_test_ext().execute_with(|| {
// Block Number is zero at genesis
assert!(System::block_number().is_zero());
System::on_created_account(Default::default());
assert!(System::events().is_empty());
// Events will be emitted starting on block 1
System::set_block_number(1);
System::on_created_account(Default::default());
assert!(System::events().len() == 1);
});
}
}
16 changes: 8 additions & 8 deletions frame/vesting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ mod tests {
.existential_deposit(256)
.build()
.execute_with(|| {
assert_eq!(System::block_number(), 1);
System::set_block_number(1);
let user1_free_balance = Balances::free_balance(&1);
let user2_free_balance = Balances::free_balance(&2);
let user12_free_balance = Balances::free_balance(&12);
Expand Down Expand Up @@ -521,7 +521,7 @@ mod tests {
.existential_deposit(10)
.build()
.execute_with(|| {
assert_eq!(System::block_number(), 1);
System::set_block_number(1);
let user1_free_balance = Balances::free_balance(&1);
assert_eq!(user1_free_balance, 100); // Account 1 has free balance
// Account 1 has only 5 units vested at block 1 (plus 50 unvested)
Expand All @@ -539,7 +539,7 @@ mod tests {
.existential_deposit(10)
.build()
.execute_with(|| {
assert_eq!(System::block_number(), 1);
System::set_block_number(1);
let user1_free_balance = Balances::free_balance(&1);
assert_eq!(user1_free_balance, 100); // Account 1 has free balance
// Account 1 has only 5 units vested at block 1 (plus 50 unvested)
Expand All @@ -555,7 +555,7 @@ mod tests {
.existential_deposit(10)
.build()
.execute_with(|| {
assert_eq!(System::block_number(), 1);
System::set_block_number(1);
let user1_free_balance = Balances::free_balance(&1);
assert_eq!(user1_free_balance, 100); // Account 1 has free balance
// Account 1 has only 5 units vested at block 1 (plus 50 unvested)
Expand All @@ -571,7 +571,7 @@ mod tests {
.existential_deposit(10)
.build()
.execute_with(|| {
assert_eq!(System::block_number(), 1);
System::set_block_number(1);
assert_ok!(Balances::transfer(Some(3).into(), 1, 100));
assert_ok!(Balances::transfer(Some(3).into(), 2, 100));

Expand Down Expand Up @@ -599,7 +599,7 @@ mod tests {
.existential_deposit(256)
.build()
.execute_with(|| {
assert_eq!(System::block_number(), 1);
System::set_block_number(1);
let user12_free_balance = Balances::free_balance(&12);

assert_eq!(user12_free_balance, 2560); // Account 12 has free balance
Expand All @@ -625,7 +625,7 @@ mod tests {
.existential_deposit(256)
.build()
.execute_with(|| {
assert_eq!(System::block_number(), 1);
System::set_block_number(1);
let user3_free_balance = Balances::free_balance(&3);
let user4_free_balance = Balances::free_balance(&4);
assert_eq!(user3_free_balance, 256 * 30);
Expand Down Expand Up @@ -669,7 +669,7 @@ mod tests {
.existential_deposit(256)
.build()
.execute_with(|| {
assert_eq!(System::block_number(), 1);
System::set_block_number(1);
let user2_free_balance = Balances::free_balance(&2);
let user4_free_balance = Balances::free_balance(&4);
assert_eq!(user2_free_balance, 256 * 20);
Expand Down