|
| 1 | +use crate as pallet_session_keys_registration; |
| 2 | + |
| 3 | +use frame_support::derive_impl; |
| 4 | +use frame_support::traits::{OnFinalize, OnInitialize}; |
| 5 | +use sp_core::parameter_types; |
| 6 | +use sp_runtime::key_types::DUMMY; |
| 7 | +use sp_runtime::testing::UintAuthorityId; |
| 8 | +use sp_runtime::{BuildStorage, KeyTypeId}; |
| 9 | +use sp_staking::SessionIndex; |
| 10 | + |
| 11 | +type Block = frame_system::mocking::MockBlock<Test>; |
| 12 | + |
| 13 | +sp_runtime::impl_opaque_keys! { |
| 14 | + pub struct SessionKeys { |
| 15 | + pub foo: UintAuthorityId, |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +frame_support::construct_runtime!( |
| 20 | + pub enum Test |
| 21 | + { |
| 22 | + System: frame_system, |
| 23 | + Session: pallet_session, |
| 24 | + SessionKeysRegistration: pallet_session_keys_registration, |
| 25 | + } |
| 26 | +); |
| 27 | + |
| 28 | +type AccountId = u64; |
| 29 | + |
| 30 | +pub struct TestSessionHandler; |
| 31 | +impl pallet_session::SessionHandler<AccountId> for TestSessionHandler { |
| 32 | + const KEY_TYPE_IDS: &'static [KeyTypeId] = &[DUMMY]; |
| 33 | + |
| 34 | + fn on_genesis_session<Ks: sp_runtime::traits::OpaqueKeys>(_validators: &[(AccountId, Ks)]) {} |
| 35 | + |
| 36 | + fn on_new_session<Ks: sp_runtime::traits::OpaqueKeys>( |
| 37 | + _: bool, |
| 38 | + _: &[(AccountId, Ks)], |
| 39 | + _: &[(AccountId, Ks)], |
| 40 | + ) { |
| 41 | + } |
| 42 | + |
| 43 | + fn on_disabled(_: u32) {} |
| 44 | +} |
| 45 | + |
| 46 | +#[derive_impl(frame_system::config_preludes::TestDefaultConfig)] |
| 47 | +impl frame_system::Config for Test { |
| 48 | + type Block = Block; |
| 49 | +} |
| 50 | + |
| 51 | +parameter_types! { |
| 52 | + pub const Period: u64 = 1; |
| 53 | + pub const Offset: u64 = 0; |
| 54 | +} |
| 55 | + |
| 56 | +impl pallet_session::Config for Test { |
| 57 | + type RuntimeEvent = RuntimeEvent; |
| 58 | + type ValidatorId = u64; |
| 59 | + type ValidatorIdOf = sp_runtime::traits::ConvertInto; |
| 60 | + type ShouldEndSession = pallet_session::PeriodicSessions<Period, Offset>; |
| 61 | + type NextSessionRotation = pallet_session::PeriodicSessions<Period, Offset>; |
| 62 | + type SessionManager = (); |
| 63 | + type SessionHandler = TestSessionHandler; |
| 64 | + type Keys = SessionKeys; |
| 65 | + type DisablingStrategy = (); |
| 66 | + type WeightInfo = (); |
| 67 | +} |
| 68 | + |
| 69 | +impl pallet_session_keys_registration::Config for Test { |
| 70 | + type PalletsOrigin = OriginCaller; |
| 71 | +} |
| 72 | + |
| 73 | +pub fn new_test_ext() -> sp_io::TestExternalities { |
| 74 | + sp_tracing::try_init_simple(); |
| 75 | + let t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap(); |
| 76 | + sp_io::TestExternalities::new(t) |
| 77 | +} |
| 78 | + |
| 79 | +pub fn start_session(session_index: SessionIndex) { |
| 80 | + for i in Session::current_index()..session_index { |
| 81 | + System::on_finalize(System::block_number()); |
| 82 | + Session::on_finalize(System::block_number()); |
| 83 | + |
| 84 | + let parent_hash = if System::block_number() > 1 { |
| 85 | + let hdr = System::finalize(); |
| 86 | + hdr.hash() |
| 87 | + } else { |
| 88 | + System::parent_hash() |
| 89 | + }; |
| 90 | + |
| 91 | + System::reset_events(); |
| 92 | + System::initialize(&(i as u64 + 1), &parent_hash, &Default::default()); |
| 93 | + System::set_block_number((i + 1).into()); |
| 94 | + |
| 95 | + System::on_initialize(System::block_number()); |
| 96 | + Session::on_initialize(System::block_number()); |
| 97 | + } |
| 98 | + |
| 99 | + assert_eq!(Session::current_index(), session_index); |
| 100 | +} |
0 commit comments