Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
59 changes: 28 additions & 31 deletions frame/babe/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//! Test utilities

use codec::Encode;
use super::{Config, Module, CurrentSlot};
use crate::{self as pallet_babe, Config, CurrentSlot};
use sp_runtime::{
Perbill, impl_opaque_keys,
curve::PiecewiseLinear,
Expand All @@ -27,7 +27,7 @@ use sp_runtime::{
};
use frame_system::InitKind;
use frame_support::{
impl_outer_dispatch, impl_outer_origin, parameter_types, StorageValue,
parameter_types, StorageValue,
traits::{KeyOwnerProofSystem, OnInitialize},
weights::Weight,
};
Expand All @@ -37,23 +37,29 @@ use sp_consensus_babe::{AuthorityId, AuthorityPair, SlotNumber};
use sp_consensus_vrf::schnorrkel::{VRFOutput, VRFProof};
use sp_staking::SessionIndex;
use pallet_staking::EraIndex;

impl_outer_origin!{
pub enum Origin for Test where system = frame_system {}
}

impl_outer_dispatch! {
pub enum Call for Test where origin: Origin {
babe::Babe,
staking::Staking,
}
}
use pallet_session::historical as pallet_session_historical;

type DummyValidatorId = u64;

// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Test;
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;

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>},
Historical: pallet_session_historical::{Module},
Offences: pallet_offences::{Module, Call, Storage, Event},
Babe: pallet_babe::{Module, Call, Storage, Config, Inherent, ValidateUnsigned},
Staking: pallet_staking::{Module, Call, Storage, Config<T>, Event<T>},
Session: pallet_session::{Module, Call, Storage, Event, Config<T>},
Timestamp: pallet_timestamp::{Module, Call, Storage, Inherent},
}
);

parameter_types! {
pub const BlockHashCount: u64 = 250;
Expand All @@ -79,9 +85,9 @@ impl frame_system::Config for Test {
type AccountId = DummyValidatorId;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Event = ();
type Event = Event;
type BlockHashCount = BlockHashCount;
type PalletInfo = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<u128>;
type OnNewAccount = ();
type OnKilledAccount = ();
Expand All @@ -104,7 +110,7 @@ impl_opaque_keys! {
}

impl pallet_session::Config for Test {
type Event = ();
type Event = Event;
type ValidatorId = <Self as frame_system::Config>::AccountId;
type ValidatorIdOf = pallet_staking::StashOf<Self>;
type ShouldEndSession = Babe;
Expand Down Expand Up @@ -151,7 +157,7 @@ impl pallet_balances::Config for Test {
type MaxLocks = ();
type Balance = u128;
type DustRemoval = ();
type Event = ();
type Event = Event;
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = System;
type WeightInfo = ();
Expand Down Expand Up @@ -182,7 +188,7 @@ parameter_types! {
impl pallet_staking::Config for Test {
type RewardRemainder = ();
type CurrencyToVote = frame_support::traits::SaturatingCurrencyToVote;
type Event = ();
type Event = Event;
type Currency = Balances;
type Slash = ();
type Reward = ();
Expand Down Expand Up @@ -210,7 +216,7 @@ parameter_types! {
}

impl pallet_offences::Config for Test {
type Event = ();
type Event = Event;
type IdentificationTuple = pallet_session::historical::IdentificationTuple<Self>;
type OnOffenceHandler = Staking;
type WeightSoftLimit = OffencesWeightSoftLimit;
Expand All @@ -235,15 +241,6 @@ impl Config for Test {
type WeightInfo = ();
}

pub type Balances = pallet_balances::Module<Test>;
pub type Historical = pallet_session::historical::Module<Test>;
pub type Offences = pallet_offences::Module<Test>;
pub type Session = pallet_session::Module<Test>;
pub type Staking = pallet_staking::Module<Test>;
pub type System = frame_system::Module<Test>;
pub type Timestamp = pallet_timestamp::Module<Test>;
pub type Babe = Module<Test>;

pub fn go_to_block(n: u64, s: u64) {
use frame_support::traits::OnFinalize;

Expand Down
50 changes: 17 additions & 33 deletions frame/balances/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,6 @@

#![cfg(test)]

#[derive(Debug)]
pub struct CallWithDispatchInfo;
impl sp_runtime::traits::Dispatchable for CallWithDispatchInfo {
type Origin = ();
type Config = ();
type Info = frame_support::weights::DispatchInfo;
type PostInfo = frame_support::weights::PostDispatchInfo;

fn dispatch(self, _origin: Self::Origin)
-> sp_runtime::DispatchResultWithInfo<Self::PostInfo> {
panic!("Do not use dummy implementation for dispatch.");
}
}

#[macro_export]
macro_rules! decl_tests {
($test:ty, $ext_builder:ty, $existential_deposit:expr) => {
Expand All @@ -52,10 +38,8 @@ macro_rules! decl_tests {
const ID_1: LockIdentifier = *b"1 ";
const ID_2: LockIdentifier = *b"2 ";

pub type System = frame_system::Module<$test>;
pub type Balances = Module<$test>;

pub const CALL: &<$test as frame_system::Config>::Call = &$crate::tests::CallWithDispatchInfo;
pub const CALL: &<$test as frame_system::Config>::Call =
&Call::Balances(pallet_balances::Call::transfer(0, 0));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This now needs to be an instance of the actual Call from construct_runtime, so just constructing a dummy transfer. Any better idea?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a better idea, but a comment in the code would be welcome. :)


/// create a transaction info struct from weight. Handy to avoid building the whole struct.
pub fn info_from_weight(w: Weight) -> DispatchInfo {
Expand Down Expand Up @@ -485,7 +469,7 @@ macro_rules! decl_tests {
assert_ok!(Balances::repatriate_reserved(&1, &2, 41, Status::Free), 0);
assert_eq!(
last_event(),
Event::balances(RawEvent::ReserveRepatriated(1, 2, 41, Status::Free)),
Event::pallet_balances(RawEvent::ReserveRepatriated(1, 2, 41, Status::Free)),
);
assert_eq!(Balances::reserved_balance(1), 69);
assert_eq!(Balances::free_balance(1), 0);
Expand Down Expand Up @@ -626,7 +610,7 @@ macro_rules! decl_tests {
fn cannot_set_genesis_value_below_ed() {
($existential_deposit).with(|v| *v.borrow_mut() = 11);
let mut t = frame_system::GenesisConfig::default().build_storage::<$test>().unwrap();
let _ = GenesisConfig::<$test> {
let _ = pallet_balances::GenesisConfig::<$test> {
balances: vec![(1, 10)],
}.assimilate_storage(&mut t).unwrap();
}
Expand All @@ -635,7 +619,7 @@ macro_rules! decl_tests {
#[should_panic = "duplicate balances in genesis."]
fn cannot_set_genesis_value_twice() {
let mut t = frame_system::GenesisConfig::default().build_storage::<$test>().unwrap();
let _ = GenesisConfig::<$test> {
let _ = pallet_balances::GenesisConfig::<$test> {
balances: vec![(1, 10), (2, 20), (1, 15)],
}.assimilate_storage(&mut t).unwrap();
}
Expand Down Expand Up @@ -704,15 +688,15 @@ macro_rules! decl_tests {

assert_eq!(
last_event(),
Event::balances(RawEvent::Reserved(1, 10)),
Event::pallet_balances(RawEvent::Reserved(1, 10)),
);

System::set_block_number(3);
let _ = Balances::unreserve(&1, 5);

assert_eq!(
last_event(),
Event::balances(RawEvent::Unreserved(1, 5)),
Event::pallet_balances(RawEvent::Unreserved(1, 5)),
);

System::set_block_number(4);
Expand All @@ -721,7 +705,7 @@ macro_rules! decl_tests {
// should only unreserve 5
assert_eq!(
last_event(),
Event::balances(RawEvent::Unreserved(1, 5)),
Event::pallet_balances(RawEvent::Unreserved(1, 5)),
);
});
}
Expand All @@ -737,9 +721,9 @@ macro_rules! decl_tests {
assert_eq!(
events(),
[
Event::system(system::Event::NewAccount(1)),
Event::balances(RawEvent::Endowed(1, 100)),
Event::balances(RawEvent::BalanceSet(1, 100, 0)),
Event::frame_system(system::Event::NewAccount(1)),
Event::pallet_balances(RawEvent::Endowed(1, 100)),
Event::pallet_balances(RawEvent::BalanceSet(1, 100, 0)),
]
);

Expand All @@ -748,8 +732,8 @@ macro_rules! decl_tests {
assert_eq!(
events(),
[
Event::balances(RawEvent::DustLost(1, 99)),
Event::system(system::Event::KilledAccount(1))
Event::pallet_balances(RawEvent::DustLost(1, 99)),
Event::frame_system(system::Event::KilledAccount(1))
]
);
});
Expand All @@ -766,9 +750,9 @@ macro_rules! decl_tests {
assert_eq!(
events(),
[
Event::system(system::Event::NewAccount(1)),
Event::balances(RawEvent::Endowed(1, 100)),
Event::balances(RawEvent::BalanceSet(1, 100, 0)),
Event::frame_system(system::Event::NewAccount(1)),
Event::pallet_balances(RawEvent::Endowed(1, 100)),
Event::pallet_balances(RawEvent::BalanceSet(1, 100, 0)),
]
);

Expand All @@ -777,7 +761,7 @@ macro_rules! decl_tests {
assert_eq!(
events(),
[
Event::system(system::Event::KilledAccount(1))
Event::frame_system(system::Event::KilledAccount(1))
]
);
});
Expand Down
43 changes: 20 additions & 23 deletions frame/balances/src/tests_composite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,27 @@ use sp_runtime::{
};
use sp_core::H256;
use sp_io;
use frame_support::{impl_outer_origin, impl_outer_event, parameter_types};
use frame_support::parameter_types;
use frame_support::weights::{Weight, DispatchInfo, IdentityFee};
use pallet_transaction_payment::CurrencyAdapter;
use crate::{GenesisConfig, Module, Config, decl_tests, tests::CallWithDispatchInfo};

use frame_system as system;
impl_outer_origin!{
pub enum Origin for Test {}
}

mod balances {
pub use crate::Event;
}
use crate::{
self as pallet_balances,
Module, Config, decl_tests,
};
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;

impl_outer_event! {
pub enum Event for Test {
system<T>,
balances<T>,
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>},
}
}
);

// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Test;
parameter_types! {
pub const BlockHashCount: u64 = 250;
pub BlockWeights: frame_system::limits::BlockWeights =
Expand All @@ -63,7 +60,7 @@ impl frame_system::Config for Test {
type Origin = Origin;
type Index = u64;
type BlockNumber = u64;
type Call = CallWithDispatchInfo;
type Call = Call;
type Hash = H256;
type Hashing = ::sp_runtime::traits::BlakeTwo256;
type AccountId = u64;
Expand All @@ -72,7 +69,7 @@ impl frame_system::Config for Test {
type Event = Event;
type BlockHashCount = BlockHashCount;
type Version = ();
type PalletInfo = ();
type PalletInfo = PalletInfo;
type AccountData = super::AccountData<u64>;
type OnNewAccount = ();
type OnKilledAccount = ();
Expand All @@ -94,7 +91,7 @@ impl Config for Test {
type DustRemoval = ();
type Event = Event;
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = system::Module<Test>;
type AccountStore = frame_system::Pallet<Test>;
type MaxLocks = ();
type WeightInfo = ();
}
Expand Down Expand Up @@ -126,7 +123,7 @@ impl ExtBuilder {
pub fn build(self) -> sp_io::TestExternalities {
self.set_associated_consts();
let mut t = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap();
GenesisConfig::<Test> {
pallet_balances::GenesisConfig::<Test> {
balances: if self.monied {
vec![
(1, 10 * self.existential_deposit),
Expand Down
Loading