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 1 commit
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
Prev Previous commit
Next Next commit
Migrate contracts tests to use construct_runtime
  • Loading branch information
ascjones committed Feb 4, 2021
commit 65e50b9175c9e7898dba47b273f83c00a5384297
4 changes: 2 additions & 2 deletions frame/contracts/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ mod tests {
RawEvent, Vm, ReturnFlags, ExecError, ErrorOrigin, AccountIdOf,
};
use crate::{
gas::GasMeter, tests::{ExtBuilder, Test, MetaEvent},
gas::GasMeter, tests::{ExtBuilder, Test, Event as MetaEvent},
exec::ExecReturnValue, CodeHash, ConfigCache,
gas::Gas,
storage::Storage,
Expand All @@ -743,7 +743,7 @@ mod tests {
<frame_system::Module<Test>>::events()
.into_iter()
.filter_map(|meta| match meta.event {
MetaEvent::contracts(contract_event) => Some(contract_event),
MetaEvent::pallet_contracts(contract_event) => Some(contract_event),
_ => None,
})
.collect()
Expand Down
118 changes: 51 additions & 67 deletions frame/contracts/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// limitations under the License.

use crate::{
BalanceOf, ContractInfo, ContractInfoOf, GenesisConfig, Module,
BalanceOf, ContractInfo, ContractInfoOf, Module,
RawAliveContractInfo, RawEvent, Config, Schedule, gas::Gas,
Error, ConfigCache, RuntimeReturnCode, storage::Storage,
chain_extension::{
Expand All @@ -34,41 +34,33 @@ use sp_runtime::{
};
use sp_io::hashing::blake2_256;
use frame_support::{
assert_ok, assert_err, assert_err_ignore_postinfo, impl_outer_dispatch, impl_outer_event,
impl_outer_origin, parameter_types, StorageMap, assert_storage_noop,
assert_ok, assert_err, assert_err_ignore_postinfo,
parameter_types, StorageMap, assert_storage_noop,
traits::{Currency, ReservableCurrency, OnInitialize},
weights::{Weight, PostDispatchInfo, DispatchClass, constants::WEIGHT_PER_SECOND},
dispatch::DispatchErrorWithPostInfo,
storage::child,
};
use frame_system::{self as system, EventRecord, Phase};

mod contracts {
// Re-export contents of the root. This basically
// needs to give a name for the current crate.
// This hack is required for `impl_outer_event!`.
pub use super::super::*;
pub use frame_support::impl_outer_event;
}
use crate as pallet_contracts;

use pallet_balances as balances;
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;

impl_outer_event! {
pub enum MetaEvent for Test {
system<T>,
balances<T>,
contracts<T>,
}
}
impl_outer_origin! {
pub enum Origin for Test where system = frame_system { }
}
impl_outer_dispatch! {
pub enum Call for Test where origin: Origin {
balances::Balances,
contracts::Contracts,
frame_support::construct_runtime!(
pub enum Test where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system::{Module, Call, Config, Storage, Event<T>},
Balances: pallet_balances::{Module, Call, Storage, Config<T>, Event<T>},
Timestamp: pallet_timestamp::{Module, Call, Storage, Inherent},
Randomness: pallet_randomness_collective_flip::{Module, Call, Storage},
Contracts: pallet_contracts::{Module, Call, Config<T>, Storage, Event<T>},
}
}
);

#[macro_use]
pub mod test_utils {
Expand Down Expand Up @@ -189,8 +181,6 @@ impl ChainExtension<Test> for TestExtension {
}
}

#[derive(Clone, Eq, PartialEq, Debug)]
pub struct Test;
parameter_types! {
pub const BlockHashCount: u64 = 250;
pub BlockWeights: frame_system::limits::BlockWeights =
Expand All @@ -211,10 +201,10 @@ impl frame_system::Config for Test {
type AccountId = AccountId32;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Event = MetaEvent;
type Event = Event;
type BlockHashCount = BlockHashCount;
type Version = ();
type PalletInfo = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<u64>;
type OnNewAccount = ();
type OnKilledAccount = ();
Expand All @@ -224,7 +214,7 @@ impl frame_system::Config for Test {
impl pallet_balances::Config for Test {
type MaxLocks = ();
type Balance = u64;
type Event = MetaEvent;
type Event = Event;
type DustRemoval = ();
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = System;
Expand Down Expand Up @@ -267,7 +257,7 @@ impl Config for Test {
type Time = Timestamp;
type Randomness = Randomness;
type Currency = Balances;
type Event = MetaEvent;
type Event = Event;
type RentPayment = ();
type SignedClaimHandicap = SignedClaimHandicap;
type TombstoneDeposit = TombstoneDeposit;
Expand All @@ -285,12 +275,6 @@ impl Config for Test {
type DeletionWeightLimit = DeletionWeightLimit;
}

type Balances = pallet_balances::Module<Test>;
type Timestamp = pallet_timestamp::Module<Test>;
type Contracts = Module<Test>;
type System = frame_system::Module<Test>;
type Randomness = pallet_randomness_collective_flip::Module<Test>;

pub const ALICE: AccountId32 = AccountId32::new([1u8; 32]);
pub const BOB: AccountId32 = AccountId32::new([2u8; 32]);
pub const CHARLIE: AccountId32 = AccountId32::new([3u8; 32]);
Expand Down Expand Up @@ -322,7 +306,7 @@ impl ExtBuilder {
pallet_balances::GenesisConfig::<Test> {
balances: vec![],
}.assimilate_storage(&mut t).unwrap();
GenesisConfig {
pallet_contracts::GenesisConfig {
current_schedule: Schedule::<Test> {
enable_println: true,
..Default::default()
Expand Down Expand Up @@ -476,50 +460,50 @@ fn instantiate_and_call_and_deposit_event() {
pretty_assertions::assert_eq!(System::events(), vec![
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::system(frame_system::Event::NewAccount(ALICE.clone())),
event: Event::frame_system(frame_system::Event::NewAccount(ALICE.clone())),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::balances(
event: Event::pallet_balances(
pallet_balances::RawEvent::Endowed(ALICE, 1_000_000)
),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::contracts(RawEvent::CodeStored(code_hash.into())),
event: Event::pallet_contracts(RawEvent::CodeStored(code_hash.into())),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::system(frame_system::Event::NewAccount(addr.clone())),
event: Event::frame_system(frame_system::Event::NewAccount(addr.clone())),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::balances(
event: Event::pallet_balances(
pallet_balances::RawEvent::Endowed(addr.clone(), subsistence * 3)
),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::balances(
event: Event::pallet_balances(
pallet_balances::RawEvent::Transfer(ALICE, addr.clone(), subsistence * 3)
),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::contracts(
event: Event::pallet_contracts(
RawEvent::ContractEmitted(addr.clone(), vec![1, 2, 3, 4])
),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::contracts(RawEvent::Instantiated(ALICE, addr.clone())),
event: Event::pallet_contracts(RawEvent::Instantiated(ALICE, addr.clone())),
topics: vec![],
}
]);
Expand Down Expand Up @@ -654,19 +638,19 @@ fn test_set_rent_code_and_hash() {
assert_eq!(System::events(), vec![
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::system(frame_system::Event::NewAccount(ALICE)),
event: Event::frame_system(frame_system::Event::NewAccount(ALICE)),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::balances(pallet_balances::RawEvent::Endowed(
event: Event::pallet_balances(pallet_balances::RawEvent::Endowed(
ALICE, 1_000_000
)),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::contracts(RawEvent::CodeStored(code_hash.into())),
event: Event::pallet_contracts(RawEvent::CodeStored(code_hash.into())),
topics: vec![],
},
]);
Expand Down Expand Up @@ -1236,22 +1220,22 @@ fn restoration(test_different_storage: bool, test_restore_to_with_dirty_storage:
assert_eq!(System::events(), vec![
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::system(frame_system::Event::NewAccount(ALICE)),
event: Event::frame_system(frame_system::Event::NewAccount(ALICE)),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::balances(pallet_balances::RawEvent::Endowed(ALICE, 1_000_000)),
event: Event::pallet_balances(pallet_balances::RawEvent::Endowed(ALICE, 1_000_000)),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::contracts(RawEvent::CodeStored(restoration_code_hash.into())),
event: Event::pallet_contracts(RawEvent::CodeStored(restoration_code_hash.into())),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::contracts(RawEvent::CodeStored(set_rent_code_hash.into())),
event: Event::pallet_contracts(RawEvent::CodeStored(set_rent_code_hash.into())),
topics: vec![],
},
]);
Expand Down Expand Up @@ -1300,7 +1284,7 @@ fn restoration(test_different_storage: bool, test_restore_to_with_dirty_storage:
assert_eq!(System::events(), vec![
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::contracts(
event: Event::pallet_contracts(
RawEvent::Evicted(addr_bob.clone())
),
topics: vec![],
Expand Down Expand Up @@ -1386,39 +1370,39 @@ fn restoration(test_different_storage: bool, test_restore_to_with_dirty_storage:
pretty_assertions::assert_eq!(System::events(), vec![
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::contracts(RawEvent::Evicted(addr_bob)),
event: Event::pallet_contracts(RawEvent::Evicted(addr_bob)),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::system(frame_system::Event::NewAccount(CHARLIE)),
event: Event::frame_system(frame_system::Event::NewAccount(CHARLIE)),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::balances(pallet_balances::RawEvent::Endowed(CHARLIE, 1_000_000)),
event: Event::pallet_balances(pallet_balances::RawEvent::Endowed(CHARLIE, 1_000_000)),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::system(frame_system::Event::NewAccount(addr_django.clone())),
event: Event::frame_system(frame_system::Event::NewAccount(addr_django.clone())),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::balances(pallet_balances::RawEvent::Endowed(addr_django.clone(), 30_000)),
event: Event::pallet_balances(pallet_balances::RawEvent::Endowed(addr_django.clone(), 30_000)),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::balances(
event: Event::pallet_balances(
pallet_balances::RawEvent::Transfer(CHARLIE, addr_django.clone(), 30_000)
),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::contracts(RawEvent::Instantiated(CHARLIE, addr_django.clone())),
event: Event::pallet_contracts(RawEvent::Instantiated(CHARLIE, addr_django.clone())),
topics: vec![],
},
]);
Expand All @@ -1441,12 +1425,12 @@ fn restoration(test_different_storage: bool, test_restore_to_with_dirty_storage:
assert_eq!(System::events(), vec![
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::system(system::Event::KilledAccount(addr_django.clone())),
event: Event::frame_system(system::Event::KilledAccount(addr_django.clone())),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::contracts(
event: Event::pallet_contracts(
RawEvent::Restored(addr_django, addr_bob, bob_contract.code_hash, 50)
),
topics: vec![],
Expand Down Expand Up @@ -1672,21 +1656,21 @@ fn self_destruct_works() {
pretty_assertions::assert_eq!(System::events(), vec![
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::system(
event: Event::frame_system(
frame_system::Event::KilledAccount(addr.clone())
),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::balances(
event: Event::pallet_balances(
pallet_balances::RawEvent::Transfer(addr.clone(), DJANGO, 100_000)
),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::contracts(
event: Event::pallet_contracts(
RawEvent::Terminated(addr.clone(), DJANGO)
),
topics: vec![],
Expand Down