use super::*;
use crate as collator_selection;
use pezframe_support::{
derive_impl, ord_parameter_types, parameter_types,
traits::{ConstBool, ConstU32, ConstU64, FindAuthor, ValidatorRegistration},
PalletId,
};
use pezframe_system as system;
use pezframe_system::EnsureSignedBy;
use pezsp_runtime::{testing::UintAuthorityId, traits::OpaqueKeys, BuildStorage, RuntimeAppPublic};
type Block = pezframe_system::mocking::MockBlock<Test>;
pezframe_support::construct_runtime!(
pub enum Test
{
System: pezframe_system,
Timestamp: pezpallet_timestamp,
Session: pezpallet_session,
Aura: pezpallet_aura,
Balances: pezpallet_balances,
CollatorSelection: collator_selection,
Authorship: pezpallet_authorship,
}
);
parameter_types! {
pub const SS58Prefix: u8 = 42;
}
#[derive_impl(pezframe_system::config_preludes::TestDefaultConfig)]
impl system::Config for Test {
type Block = Block;
type AccountData = pezpallet_balances::AccountData<u64>;
type SS58Prefix = SS58Prefix;
}
parameter_types! {
pub const ExistentialDeposit: u64 = 5;
}
#[derive_impl(pezpallet_balances::config_preludes::TestDefaultConfig)]
impl pezpallet_balances::Config for Test {
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = System;
}
pub struct Author4;
impl FindAuthor<u64> for Author4 {
fn find_author<'a, I>(_digests: I) -> Option<u64>
where
I: 'a + IntoIterator<Item = (pezframe_support::ConsensusEngineId, &'a [u8])>,
{
Some(4)
}
}
impl pezpallet_authorship::Config for Test {
type FindAuthor = Author4;
type EventHandler = CollatorSelection;
}
impl pezpallet_timestamp::Config for Test {
type Moment = u64;
type OnTimestampSet = Aura;
type MinimumPeriod = ConstU64<1>;
type WeightInfo = ();
}
impl pezpallet_aura::Config for Test {
type AuthorityId = pezsp_consensus_aura::sr25519::AuthorityId;
type MaxAuthorities = ConstU32<100_000>;
type DisabledValidators = ();
type AllowMultipleBlocksPerSlot = ConstBool<false>;
type SlotDuration = pezpallet_aura::MinimumPeriodTimesTwo<Self>;
}
pezsp_runtime::impl_opaque_keys! {
pub struct MockSessionKeys {
pub aura: UintAuthorityId,
}
}
impl From<UintAuthorityId> for MockSessionKeys {
fn from(aura: pezsp_runtime::testing::UintAuthorityId) -> Self {
Self { aura }
}
}
parameter_types! {
pub static SessionHandlerCollators: Vec<u64> = Vec::new();
pub static SessionChangeBlock: u64 = 0;
}
pub struct TestSessionHandler;
impl pezpallet_session::SessionHandler<u64> for TestSessionHandler {
const KEY_TYPE_IDS: &'static [pezsp_runtime::KeyTypeId] = &[UintAuthorityId::ID];
fn on_genesis_session<Ks: OpaqueKeys>(keys: &[(u64, Ks)]) {
SessionHandlerCollators::set(keys.iter().map(|(a, _)| *a).collect::<Vec<_>>())
}
fn on_new_session<Ks: OpaqueKeys>(_: bool, keys: &[(u64, Ks)], _: &[(u64, Ks)]) {
SessionChangeBlock::set(System::block_number());
SessionHandlerCollators::set(keys.iter().map(|(a, _)| *a).collect::<Vec<_>>())
}
fn on_before_session_ending() {}
fn on_disabled(_: u32) {}
}
parameter_types! {
pub const Offset: u64 = 0;
pub const Period: u64 = 10;
}
impl pezpallet_session::Config for Test {
type RuntimeEvent = RuntimeEvent;
type ValidatorId = <Self as pezframe_system::Config>::AccountId;
type ValidatorIdOf = IdentityCollator;
type ShouldEndSession = pezpallet_session::PeriodicSessions<Period, Offset>;
type NextSessionRotation = pezpallet_session::PeriodicSessions<Period, Offset>;
type SessionManager = CollatorSelection;
type SessionHandler = TestSessionHandler;
type Keys = MockSessionKeys;
type DisablingStrategy = ();
type WeightInfo = ();
type Currency = Balances;
type KeyDeposit = ();
}
ord_parameter_types! {
pub const RootAccount: u64 = 777;
}
parameter_types! {
pub const PotId: PalletId = PalletId(*b"PotStake");
}
pub struct IsRegistered;
impl ValidatorRegistration<u64> for IsRegistered {
fn is_registered(id: &u64) -> bool {
*id != 42u64
}
}
impl Config for Test {
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type UpdateOrigin = EnsureSignedBy<RootAccount, u64>;
type PotId = PotId;
type MaxCandidates = ConstU32<20>;
type MinEligibleCollators = ConstU32<1>;
type MaxInvulnerables = ConstU32<20>;
type KickThreshold = Period;
type ValidatorId = <Self as pezframe_system::Config>::AccountId;
type ValidatorIdOf = IdentityCollator;
type ValidatorRegistration = IsRegistered;
type WeightInfo = ();
}
pub fn new_test_ext() -> pezsp_io::TestExternalities {
pezsp_tracing::try_init_simple();
let mut t = pezframe_system::GenesisConfig::<Test>::default().build_storage().unwrap();
let invulnerables = vec![2, 1];
let balances = vec![(1, 100), (2, 100), (3, 100), (4, 100), (5, 100)];
let keys = balances
.iter()
.map(|&(i, _)| (i, i, MockSessionKeys { aura: UintAuthorityId(i) }))
.collect::<Vec<_>>();
let collator_selection = collator_selection::GenesisConfig::<Test> {
desired_candidates: 2,
candidacy_bond: 10,
invulnerables,
};
let session = pezpallet_session::GenesisConfig::<Test> { keys, ..Default::default() };
pezpallet_balances::GenesisConfig::<Test> { balances, ..Default::default() }
.assimilate_storage(&mut t)
.unwrap();
collator_selection.assimilate_storage(&mut t).unwrap();
session.assimilate_storage(&mut t).unwrap();
t.into()
}
pub fn initialize_to_block(n: u64) {
for i in System::block_number() + 1..=n {
System::set_block_number(i);
<AllPalletsWithSystem as pezframe_support::traits::OnInitialize<u64>>::on_initialize(i);
}
}