use frame_support::traits::GenesisBuild; use sp_std::marker::PhantomData; use frame_support::traits::OriginTrait; use parachains_common::AccountId; use sp_consensus_aura::AURA_ENGINE_ID; use sp_core::Encode; use sp_runtime::{Digest, DigestItem}; pub type BalanceOf = ::Balance; pub type AccountIdOf = ::AccountId; pub type ValidatorIdOf = ::ValidatorId; pub type SessionKeysOf = ::Keys; // Basic builder based on balances, collators and pallet_sessopm pub struct ExtBuilder< Runtime: frame_system::Config + pallet_balances::Config + pallet_session::Config, > { // endowed accounts with balances balances: Vec<(AccountIdOf, BalanceOf)>, // collators to test block prod collators: Vec>, // keys added to pallet session keys: Vec<(AccountIdOf, ValidatorIdOf, SessionKeysOf)>, _runtime: PhantomData, } impl Default for ExtBuilder { fn default() -> ExtBuilder { ExtBuilder { balances: vec![], collators: vec![], keys: vec![], _runtime: PhantomData } } } impl ExtBuilder { pub fn with_balances( mut self, balances: Vec<(AccountIdOf, BalanceOf)>, ) -> Self { self.balances = balances; self } pub fn with_collators(mut self, collators: Vec>) -> Self { self.collators = collators; self } pub fn with_session_keys( mut self, keys: Vec<(AccountIdOf, ValidatorIdOf, SessionKeysOf)>, ) -> Self { self.keys = keys; self } pub fn build(self) -> sp_io::TestExternalities where Runtime: pallet_collator_selection::Config + pallet_balances::Config + pallet_session::Config, ValidatorIdOf: From>, { let mut t = frame_system::GenesisConfig::default().build_storage::().unwrap(); pallet_balances::GenesisConfig:: { balances: self.balances.into() } .assimilate_storage(&mut t) .unwrap(); pallet_collator_selection::GenesisConfig:: { invulnerables: self.collators.clone().into(), candidacy_bond: Default::default(), desired_candidates: Default::default(), } .assimilate_storage(&mut t) .unwrap(); pallet_session::GenesisConfig:: { keys: self.keys } .assimilate_storage(&mut t) .unwrap(); let mut ext = sp_io::TestExternalities::new(t); ext.execute_with(|| { frame_system::Pallet::::set_block_number(1u32.into()); }); ext } } pub struct RuntimeHelper(PhantomData); /// Utility function that advances the chain to the desired block number. /// If an author is provided, that author information is injected to all the blocks in the meantime. impl RuntimeHelper where AccountIdOf: Into<<::RuntimeOrigin as OriginTrait>::AccountId>, { pub fn run_to_block(n: u32, author: Option) { while frame_system::Pallet::::block_number() < n.into() { // Set the new block number and author match author { Some(ref author) => { let pre_digest = Digest { logs: vec![DigestItem::PreRuntime(AURA_ENGINE_ID, author.encode())], }; frame_system::Pallet::::reset_events(); frame_system::Pallet::::initialize( &(frame_system::Pallet::::block_number() + 1u32.into()), &frame_system::Pallet::::parent_hash(), &pre_digest, ); }, None => { frame_system::Pallet::::set_block_number( frame_system::Pallet::::block_number() + 1u32.into(), ); }, } } } pub fn root_origin() -> ::RuntimeOrigin { ::RuntimeOrigin::root() } pub fn origin_of( account_id: AccountIdOf, ) -> ::RuntimeOrigin { ::RuntimeOrigin::signed(account_id.into()) } }