123456789101112131415161718192021222324252627282930313233use super::*;34use crate as collator_selection;35use frame_support::{36 ord_parameter_types, parameter_types,37 traits::{FindAuthor, GenesisBuild, ValidatorRegistration},38 PalletId,39};40use frame_system as system;41use frame_system::EnsureSignedBy;42use sp_core::H256;43use sp_runtime::{44 testing::{Header, UintAuthorityId},45 traits::{BlakeTwo256, IdentityLookup, OpaqueKeys},46 Perbill, RuntimeAppPublic,47};4849type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;50type Block = frame_system::mocking::MockBlock<Test>;515253frame_support::construct_runtime!(54 pub enum Test where55 Block = Block,56 NodeBlock = Block,57 UncheckedExtrinsic = UncheckedExtrinsic,58 {59 System: frame_system::{Pallet, Call, Config, Storage, Event<T>},60 Timestamp: pallet_timestamp::{Pallet, Call, Storage, Inherent},61 Session: pallet_session::{Pallet, Call, Storage, Event, Config<T>},62 Aura: pallet_aura::{Pallet, Storage, Config<T>},63 Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},64 CollatorSelection: collator_selection::{Pallet, Call, Storage, Event<T>},65 Authorship: pallet_authorship::{Pallet, Storage},66 Configuration: pallet_configuration::{Pallet, Call, Storage, Event<T>},67 }68);6970parameter_types! {71 pub const BlockHashCount: u64 = 250;72 pub const SS58Prefix: u8 = 42;73}7475impl system::Config for Test {76 type BaseCallFilter = frame_support::traits::Everything;77 type BlockWeights = ();78 type BlockLength = ();79 type DbWeight = ();80 type RuntimeOrigin = RuntimeOrigin;81 type RuntimeCall = RuntimeCall;82 type Index = u64;83 type BlockNumber = u64;84 type Hash = H256;85 type Hashing = BlakeTwo256;86 type AccountId = u64;87 type Lookup = IdentityLookup<Self::AccountId>;88 type Header = Header;89 type RuntimeEvent = RuntimeEvent;90 type BlockHashCount = BlockHashCount;91 type Version = ();92 type PalletInfo = PalletInfo;93 type AccountData = pallet_balances::AccountData<u64>;94 type OnNewAccount = ();95 type OnKilledAccount = ();96 type SystemWeightInfo = ();97 type SS58Prefix = SS58Prefix;98 type OnSetCode = ();99 type MaxConsumers = frame_support::traits::ConstU32<16>;100}101102parameter_types! {103 pub const ExistentialDeposit: u64 = 5;104 pub const MaxReserves: u32 = 50;105 pub const MaxHolds: u32 = 2;106 pub const MaxFreezes: u32 = 2;107}108109impl pallet_balances::Config for Test {110 type Balance = u64;111 type RuntimeEvent = RuntimeEvent;112 type DustRemoval = ();113 type ExistentialDeposit = ExistentialDeposit;114 type AccountStore = System;115 type WeightInfo = ();116 type MaxLocks = ();117 type MaxReserves = MaxReserves;118 type ReserveIdentifier = [u8; 8];119 type HoldIdentifier = [u8; 16];120 type FreezeIdentifier = [u8; 16];121 type MaxHolds = MaxHolds;122 type MaxFreezes = MaxFreezes;123}124125pub struct Author4;126impl FindAuthor<u64> for Author4 {127 fn find_author<'a, I>(_digests: I) -> Option<u64>128 where129 I: 'a + IntoIterator<Item = (frame_support::ConsensusEngineId, &'a [u8])>,130 {131 Some(4)132 }133}134135impl pallet_authorship::Config for Test {136 type FindAuthor = Author4;137 type EventHandler = CollatorSelection;138}139140parameter_types! {141 pub const MinimumPeriod: u64 = 1;142}143144impl pallet_timestamp::Config for Test {145 type Moment = u64;146 type OnTimestampSet = Aura;147 type MinimumPeriod = MinimumPeriod;148 type WeightInfo = ();149}150151impl pallet_aura::Config for Test {152 type AuthorityId = sp_consensus_aura::sr25519::AuthorityId;153 type MaxAuthorities = MaxAuthorities;154 type DisabledValidators = ();155}156157sp_runtime::impl_opaque_keys! {158 pub struct MockSessionKeys {159 160 pub aura: UintAuthorityId,161 }162}163164impl From<UintAuthorityId> for MockSessionKeys {165 fn from(aura: sp_runtime::testing::UintAuthorityId) -> Self {166 Self { aura }167 }168}169170parameter_types! {171 pub static SessionHandlerCollators: Vec<u64> = Vec::new();172 pub static SessionChangeBlock: u64 = 0;173}174175pub struct TestSessionHandler;176impl pallet_session::SessionHandler<u64> for TestSessionHandler {177 const KEY_TYPE_IDS: &'static [sp_runtime::KeyTypeId] = &[UintAuthorityId::ID];178 fn on_genesis_session<Ks: OpaqueKeys>(keys: &[(u64, Ks)]) {179 SessionHandlerCollators::set(keys.into_iter().map(|(a, _)| *a).collect::<Vec<_>>())180 }181 fn on_new_session<Ks: OpaqueKeys>(_: bool, keys: &[(u64, Ks)], _: &[(u64, Ks)]) {182 SessionChangeBlock::set(System::block_number());183 dbg!(keys.len());184 SessionHandlerCollators::set(keys.into_iter().map(|(a, _)| *a).collect::<Vec<_>>())185 }186 fn on_before_session_ending() {}187 fn on_disabled(_: u32) {}188}189190parameter_types! {191 pub const Offset: u64 = 0;192 pub const Period: u64 = 10;193}194195impl pallet_session::Config for Test {196 type RuntimeEvent = RuntimeEvent;197 type ValidatorId = <Self as frame_system::Config>::AccountId;198 199 type ValidatorIdOf = IdentityCollator;200 type ShouldEndSession = pallet_session::PeriodicSessions<Period, Offset>;201 type NextSessionRotation = pallet_session::PeriodicSessions<Period, Offset>;202 type SessionManager = CollatorSelection;203 type SessionHandler = TestSessionHandler;204 type Keys = MockSessionKeys;205 type WeightInfo = ();206}207208parameter_types! {209 pub const MaxCollators: u32 = 5;210 pub const LicenseBond: u64 = 10;211 pub const KickThreshold: u64 = 10;212 213 pub const DefaultWeightToFeeCoefficient: u64 = 100_000;214 pub const DefaultMinGasPrice: u64 = 100_000;215 pub const MaxXcmAllowedLocations: u32 = 16;216 pub AppPromotionDailyRate: Perbill = Perbill::from_rational(5u32, 10_000);217 pub const DayRelayBlocks: u32 = 1;218 pub const LicenceBondIdentifier: [u8; 16] = *b"licenceidentifie";219}220221impl pallet_configuration::Config for Test {222 type RuntimeEvent = RuntimeEvent;223 type Currency = Balances;224 type DefaultCollatorSelectionMaxCollators = MaxCollators;225 type DefaultCollatorSelectionKickThreshold = KickThreshold;226 type DefaultCollatorSelectionLicenseBond = LicenseBond;227 228 type DefaultWeightToFeeCoefficient = DefaultWeightToFeeCoefficient;229 type DefaultMinGasPrice = DefaultMinGasPrice;230 type MaxXcmAllowedLocations = MaxXcmAllowedLocations;231 type AppPromotionDailyRate = AppPromotionDailyRate;232 type DayRelayBlocks = DayRelayBlocks;233 type WeightInfo = pallet_configuration::weights::SubstrateWeight<Self>;234}235236ord_parameter_types! {237 pub const RootAccount: u64 = 777;238}239240parameter_types! {241 pub const PotId: PalletId = PalletId(*b"PotStake");242 pub const MaxAuthorities: u32 = 100_000;243 pub const SlashRatio: Perbill = Perbill::one();244}245246pub struct IsRegistered;247impl ValidatorRegistration<u64> for IsRegistered {248 fn is_registered(id: &u64) -> bool {249 if *id == 7u64 {250 false251 } else {252 true253 }254 }255}256257impl Config for Test {258 type RuntimeEvent = RuntimeEvent;259 type UpdateOrigin = EnsureSignedBy<RootAccount, u64>;260 type PotId = PotId;261 type MaxCollators = MaxCollators;262 type SlashRatio = SlashRatio;263 type TreasuryAccountId = ();264 type ValidatorId = <Self as frame_system::Config>::AccountId;265 type ValidatorIdOf = IdentityCollator;266 type ValidatorRegistration = IsRegistered;267 type LicenceBondIdentifier = LicenceBondIdentifier;268 type WeightInfo = ();269}270271pub fn new_test_ext() -> sp_io::TestExternalities {272 sp_tracing::try_init_simple();273 let mut t = frame_system::GenesisConfig::default()274 .build_storage::<Test>()275 .unwrap();276 let invulnerables = vec![1, 2];277278 let ed = <Test as pallet_balances::Config>::ExistentialDeposit::get();279280 let balances = vec![(1, 100), (2, 100), (3, 100), (4, 100), (5, 100), (33, ed)];281 let keys = balances282 .iter()283 .map(|&(i, _)| {284 (285 i,286 i,287 MockSessionKeys {288 aura: UintAuthorityId(i),289 },290 )291 })292 .collect::<Vec<_>>();293 let collator_selection = collator_selection::GenesisConfig::<Test> { invulnerables };294 let session = pallet_session::GenesisConfig::<Test> { keys };295 pallet_balances::GenesisConfig::<Test> { balances }296 .assimilate_storage(&mut t)297 .unwrap();298 299 collator_selection.assimilate_storage(&mut t).unwrap();300 session.assimilate_storage(&mut t).unwrap();301302 t.into()303}304305pub fn initialize_to_block(n: u64) {306 for i in System::block_number() + 1..=n {307 System::set_block_number(i);308 <AllPalletsWithSystem as frame_support::traits::OnInitialize<u64>>::on_initialize(i);309 }310}