123456789101112131415161718192021222324252627282930313233use frame_support::{34 ord_parameter_types, parameter_types,35 traits::{FindAuthor, GenesisBuild, ValidatorRegistration},36 PalletId,37};38use frame_system as system;39use frame_system::EnsureSignedBy;40use sp_core::H256;41use sp_runtime::{42 testing::{Header, UintAuthorityId},43 traits::{BlakeTwo256, IdentityLookup, OpaqueKeys},44 Perbill, RuntimeAppPublic,45};4647use super::*;48use crate as collator_selection;4950type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;51type Block = frame_system::mocking::MockBlock<Test>;525354frame_support::construct_runtime!(55 pub enum Test {56 System: frame_system,57 Timestamp: pallet_timestamp,58 Session: pallet_session,59 Aura: pallet_aura,60 Balances: pallet_balances,61 CollatorSelection: collator_selection,62 Authorship: pallet_authorship,63 }64);6566parameter_types! {67 pub const BlockHashCount: u64 = 250;68 pub const SS58Prefix: u8 = 42;69}7071impl system::Config for Test {72 type BaseCallFilter = frame_support::traits::Everything;73 type BlockWeights = ();74 type BlockLength = ();75 type DbWeight = ();76 type RuntimeOrigin = RuntimeOrigin;77 type RuntimeCall = RuntimeCall;78 type Nonce = u64;79 type Hash = H256;80 type Hashing = BlakeTwo256;81 type AccountId = u64;82 type Lookup = IdentityLookup<Self::AccountId>;83 type RuntimeEvent = RuntimeEvent;84 type BlockHashCount = BlockHashCount;85 type Version = ();86 type PalletInfo = PalletInfo;87 type AccountData = pallet_balances::AccountData<u64>;88 type OnNewAccount = ();89 type OnKilledAccount = ();90 type SystemWeightInfo = ();91 type SS58Prefix = SS58Prefix;92 type OnSetCode = ();93 type MaxConsumers = frame_support::traits::ConstU32<16>;94}9596parameter_types! {97 pub const ExistentialDeposit: u64 = 5;98 pub const MaxReserves: u32 = 50;99 pub const MaxHolds: u32 = 2;100 pub const MaxFreezes: u32 = 2;101}102103impl pallet_balances::Config for Test {104 type Balance = u64;105 type RuntimeEvent = RuntimeEvent;106 type DustRemoval = ();107 type ExistentialDeposit = ExistentialDeposit;108 type AccountStore = System;109 type WeightInfo = ();110 type MaxLocks = ();111 type MaxReserves = MaxReserves;112 type ReserveIdentifier = [u8; 8];113 type FreezeIdentifier = [u8; 16];114 type MaxHolds = MaxHolds;115 type MaxFreezes = MaxFreezes;116}117118pub struct Author4;119impl FindAuthor<u64> for Author4 {120 fn find_author<'a, I>(_digests: I) -> Option<u64>121 where122 I: 'a + IntoIterator<Item = (frame_support::ConsensusEngineId, &'a [u8])>,123 {124 Some(4)125 }126}127128impl pallet_authorship::Config for Test {129 type FindAuthor = Author4;130 type EventHandler = CollatorSelection;131}132133parameter_types! {134 pub const MinimumPeriod: u64 = 1;135}136137impl pallet_timestamp::Config for Test {138 type Moment = u64;139 type OnTimestampSet = Aura;140 type MinimumPeriod = MinimumPeriod;141 type WeightInfo = ();142}143144impl pallet_aura::Config for Test {145 type AuthorityId = sp_consensus_aura::sr25519::AuthorityId;146 type MaxAuthorities = MaxAuthorities;147 type DisabledValidators = ();148}149150sp_runtime::impl_opaque_keys! {151 pub struct MockSessionKeys {152 153 pub aura: UintAuthorityId,154 }155}156157impl From<UintAuthorityId> for MockSessionKeys {158 fn from(aura: sp_runtime::testing::UintAuthorityId) -> Self {159 Self { aura }160 }161}162163parameter_types! {164 pub static SessionHandlerCollators: Vec<u64> = Vec::new();165 pub static SessionChangeBlock: u64 = 0;166}167168pub struct TestSessionHandler;169impl pallet_session::SessionHandler<u64> for TestSessionHandler {170 const KEY_TYPE_IDS: &'static [sp_runtime::KeyTypeId] = &[UintAuthorityId::ID];171 fn on_genesis_session<Ks: OpaqueKeys>(keys: &[(u64, Ks)]) {172 SessionHandlerCollators::set(keys.into_iter().map(|(a, _)| *a).collect::<Vec<_>>())173 }174 fn on_new_session<Ks: OpaqueKeys>(_: bool, keys: &[(u64, Ks)], _: &[(u64, Ks)]) {175 SessionChangeBlock::set(System::block_number());176 dbg!(keys.len());177 SessionHandlerCollators::set(keys.into_iter().map(|(a, _)| *a).collect::<Vec<_>>())178 }179 fn on_before_session_ending() {}180 fn on_disabled(_: u32) {}181}182183parameter_types! {184 pub const Offset: u64 = 0;185 pub const Period: u64 = 10;186}187188impl pallet_session::Config for Test {189 type RuntimeEvent = RuntimeEvent;190 type ValidatorId = <Self as frame_system::Config>::AccountId;191 192 type ValidatorIdOf = IdentityCollator;193 type ShouldEndSession = pallet_session::PeriodicSessions<Period, Offset>;194 type NextSessionRotation = pallet_session::PeriodicSessions<Period, Offset>;195 type SessionManager = CollatorSelection;196 type SessionHandler = TestSessionHandler;197 type Keys = MockSessionKeys;198 type WeightInfo = ();199}200201parameter_types! {202 pub const MaxCollators: u32 = 5;203 pub const LicenseBond: u64 = 10;204 pub const KickThreshold: u64 = 10;205 206 pub const DefaultWeightToFeeCoefficient: u64 = 100_000;207 pub const DefaultMinGasPrice: u64 = 100_000;208 pub const MaxXcmAllowedLocations: u32 = 16;209 pub AppPromotionDailyRate: Perbill = Perbill::from_rational(5u32, 10_000);210 pub const DayRelayBlocks: u32 = 1;211 pub const LicenceBondIdentifier: [u8; 16] = *b"licenceidentifie";212}213214ord_parameter_types! {215 pub const RootAccount: u64 = 777;216}217218parameter_types! {219 pub const PotId: PalletId = PalletId(*b"PotStake");220 pub const MaxAuthorities: u32 = 100_000;221 pub const SlashRatio: Perbill = Perbill::one();222}223224pub struct IsRegistered;225impl ValidatorRegistration<u64> for IsRegistered {226 fn is_registered(id: &u64) -> bool {227 *id != 7u64228 }229}230231impl Config for Test {232 type RuntimeEvent = RuntimeEvent;233 type UpdateOrigin = EnsureSignedBy<RootAccount, u64>;234 type PotId = PotId;235 type MaxCollators = MaxCollators;236 type SlashRatio = SlashRatio;237 type TreasuryAccountId = ();238 type ValidatorId = <Self as frame_system::Config>::AccountId;239 type ValidatorIdOf = IdentityCollator;240 type ValidatorRegistration = IsRegistered;241 type LicenceBondIdentifier = LicenceBondIdentifier;242 type Currency = Balances;243 type DesiredCollators = MaxCollators;244 type LicenseBond = LicenseBond;245 type KickThreshold = KickThreshold;246 type WeightInfo = ();247}248249pub fn new_test_ext() -> sp_io::TestExternalities {250 sp_tracing::try_init_simple();251 let mut t = frame_system::GenesisConfig::default()252 .build_storage::<Test>()253 .unwrap();254 let invulnerables = vec![1, 2];255256 let ed = <Test as pallet_balances::Config>::ExistentialDeposit::get();257258 let balances: Vec<(u64, u64)> = (1..=<Test as Config>::DesiredCollators::get() as u64 + 1)259 .map(|i| (i, 100))260 .chain(core::iter::once((33, ed)))261 .collect();262263 let keys = balances264 .iter()265 .map(|&(i, _)| {266 (267 i,268 i,269 MockSessionKeys {270 aura: UintAuthorityId(i),271 },272 )273 })274 .collect::<Vec<_>>();275 let collator_selection = collator_selection::GenesisConfig::<Test> { invulnerables };276 let session = pallet_session::GenesisConfig::<Test> { keys };277 pallet_balances::GenesisConfig::<Test> { balances }278 .assimilate_storage(&mut t)279 .unwrap();280 281 collator_selection.assimilate_storage(&mut t).unwrap();282 session.assimilate_storage(&mut t).unwrap();283284 t.into()285}286287pub fn initialize_to_block(n: u64) {288 for i in System::block_number() + 1..=n {289 System::set_block_number(i);290 <AllPalletsWithSystem as frame_support::traits::OnInitialize<u64>>::on_initialize(i);291 }292}