Skip to content

Commit

Permalink
test: fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
CertainLach committed Oct 9, 2023
1 parent 22a5178 commit c3c95ca
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 66 deletions.
40 changes: 21 additions & 19 deletions pallets/collator-selection/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,22 @@

use frame_support::{
ord_parameter_types, parameter_types,
traits::{FindAuthor, GenesisBuild, ValidatorRegistration},
traits::{ConstU32, FindAuthor, ValidatorRegistration},
PalletId,
};
use frame_system as system;
use frame_system::EnsureSignedBy;
use sp_core::H256;
use sp_core::{ConstBool, H256};
use sp_runtime::{
testing::{Header, UintAuthorityId},
testing::UintAuthorityId,
traits::{BlakeTwo256, IdentityLookup, OpaqueKeys},
Perbill, RuntimeAppPublic,
BuildStorage, Perbill, RuntimeAppPublic,
};

use super::*;
use crate as collator_selection;

type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;
type Block = frame_system::mocking::MockBlockU32<Test>;

// Configure a mock runtime to test the pallet.
frame_support::construct_runtime!(
Expand All @@ -64,12 +63,13 @@ frame_support::construct_runtime!(
);

parameter_types! {
pub const BlockHashCount: u64 = 250;
pub const BlockHashCount: u32 = 250;
pub const SS58Prefix: u8 = 42;
}

impl system::Config for Test {
type BaseCallFilter = frame_support::traits::Everything;
type Block = Block;
type BlockWeights = ();
type BlockLength = ();
type DbWeight = ();
Expand All @@ -90,7 +90,7 @@ impl system::Config for Test {
type SystemWeightInfo = ();
type SS58Prefix = SS58Prefix;
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
type MaxConsumers = ConstU32<16>;
}

parameter_types! {
Expand All @@ -113,6 +113,7 @@ impl pallet_balances::Config for Test {
type FreezeIdentifier = [u8; 16];
type MaxHolds = MaxHolds;
type MaxFreezes = MaxFreezes;
type RuntimeHoldReason = RuntimeHoldReason;
}

pub struct Author4;
Expand Down Expand Up @@ -145,6 +146,7 @@ impl pallet_aura::Config for Test {
type AuthorityId = sp_consensus_aura::sr25519::AuthorityId;
type MaxAuthorities = MaxAuthorities;
type DisabledValidators = ();
type AllowMultipleBlocksPerSlot = ConstBool<true>;
}

sp_runtime::impl_opaque_keys! {
Expand All @@ -162,27 +164,27 @@ impl From<UintAuthorityId> for MockSessionKeys {

parameter_types! {
pub static SessionHandlerCollators: Vec<u64> = Vec::new();
pub static SessionChangeBlock: u64 = 0;
pub static SessionChangeBlock: u32 = 0;
}

pub struct TestSessionHandler;
impl pallet_session::SessionHandler<u64> for TestSessionHandler {
const KEY_TYPE_IDS: &'static [sp_runtime::KeyTypeId] = &[UintAuthorityId::ID];
fn on_genesis_session<Ks: OpaqueKeys>(keys: &[(u64, Ks)]) {
SessionHandlerCollators::set(keys.into_iter().map(|(a, _)| *a).collect::<Vec<_>>())
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());
dbg!(keys.len());
SessionHandlerCollators::set(keys.into_iter().map(|(a, _)| *a).collect::<Vec<_>>())
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;
pub const Offset: u32 = 0;
pub const Period: u32 = 10;
}

impl pallet_session::Config for Test {
Expand All @@ -201,7 +203,7 @@ impl pallet_session::Config for Test {
parameter_types! {
pub const MaxCollators: u32 = 5;
pub const LicenseBond: u64 = 10;
pub const KickThreshold: u64 = 10;
pub const KickThreshold: u32 = 10;
// the following values do not matter and are meaningless, etc.
pub const DefaultWeightToFeeCoefficient: u64 = 100_000;
pub const DefaultMinGasPrice: u64 = 100_000;
Expand Down Expand Up @@ -230,6 +232,7 @@ impl ValidatorRegistration<u64> for IsRegistered {

impl Config for Test {
type RuntimeEvent = RuntimeEvent;
type RuntimeHoldReason = RuntimeHoldReason;
type UpdateOrigin = EnsureSignedBy<RootAccount, u64>;
type PotId = PotId;
type MaxCollators = MaxCollators;
Expand All @@ -238,7 +241,6 @@ impl Config for Test {
type ValidatorId = <Self as frame_system::Config>::AccountId;
type ValidatorIdOf = IdentityCollator;
type ValidatorRegistration = IsRegistered;
type LicenceBondIdentifier = LicenceBondIdentifier;
type Currency = Balances;
type DesiredCollators = MaxCollators;
type LicenseBond = LicenseBond;
Expand All @@ -248,8 +250,8 @@ impl Config for Test {

pub fn new_test_ext() -> sp_io::TestExternalities {
sp_tracing::try_init_simple();
let mut t = frame_system::GenesisConfig::default()
.build_storage::<Test>()
let mut t = <frame_system::GenesisConfig<Test>>::default()
.build_storage()
.unwrap();
let invulnerables = vec![1, 2];

Expand Down Expand Up @@ -284,9 +286,9 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
t.into()
}

pub fn initialize_to_block(n: u64) {
pub fn initialize_to_block(n: u32) {
for i in System::block_number() + 1..=n {
System::set_block_number(i);
<AllPalletsWithSystem as frame_support::traits::OnInitialize<u64>>::on_initialize(i);
<AllPalletsWithSystem as frame_support::traits::OnInitialize<u32>>::on_initialize(i);
}
}
8 changes: 4 additions & 4 deletions pallets/collator-selection/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@

use frame_support::{
assert_noop, assert_ok,
traits::{fungible, GenesisBuild, OnInitialize},
traits::{fungible, OnInitialize},
};
use scale_info::prelude::*;
use sp_runtime::{traits::BadOrigin, TokenError};
use sp_runtime::{traits::BadOrigin, BuildStorage, TokenError};

use crate::{self as collator_selection, mock::*, Config, Error};

Expand Down Expand Up @@ -464,8 +464,8 @@ fn kick_mechanism() {
#[should_panic = "duplicate invulnerables in genesis."]
fn cannot_set_genesis_value_twice() {
sp_tracing::try_init_simple();
let mut t = frame_system::GenesisConfig::default()
.build_storage::<Test>()
let mut t = <frame_system::GenesisConfig<Test>>::default()
.build_storage()
.unwrap();
let invulnerables = vec![1, 1];

Expand Down
6 changes: 2 additions & 4 deletions pallets/identity/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,14 @@ use frame_system::{EnsureRoot, EnsureSignedBy};
use parity_scale_codec::{Decode, Encode};
use sp_core::H256;
use sp_runtime::{
testing::Header,
traits::{BadOrigin, BlakeTwo256, IdentityLookup},
BuildStorage,
};

use super::*;
use crate as pallet_identity;

type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;
type Block = frame_system::mocking::MockBlockU32<Test>;

frame_support::construct_runtime!(
pub enum Test {
Expand All @@ -79,7 +77,7 @@ impl frame_system::Config for Test {
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = ConstU64<250>;
type BlockHashCount = ConstU32<250>;
type DbWeight = ();
type Version = ();
type PalletInfo = PalletInfo;
Expand Down
48 changes: 25 additions & 23 deletions pallets/inflation/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ use sp_runtime::{

use crate as pallet_inflation;

type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;
type Block = frame_system::mocking::MockBlockU32<Test>;

const YEAR: u64 = 5_259_600; // 6-second blocks
// const YEAR: u64 = 2_629_800; // 12-second blocks
// Expected 100-block inflation for year 1 is 100 * 100_000_000 / YEAR = FIRST_YEAR_BLOCK_INFLATION
// 6-second blocks
// const YEAR: u32 = 2_629_800; // 12-second blocks
// Expected 100-block inflation for year 1 is 100 * 100_000_000 / YEAR = FIRST_YEAR_BLOCK_INFLATION
const YEAR: u32 = 5_259_600;
const FIRST_YEAR_BLOCK_INFLATION: u64 = 1901;

parameter_types! {
Expand All @@ -60,6 +60,7 @@ impl pallet_balances::Config for Test {
type FreezeIdentifier = ();
type MaxHolds = ();
type MaxFreezes = ();
type RuntimeHoldReason = RuntimeHoldReason;
}

frame_support::construct_runtime!(
Expand All @@ -71,14 +72,15 @@ frame_support::construct_runtime!(
);

parameter_types! {
pub const BlockHashCount: u64 = 250;
pub const BlockHashCount: u32 = 250;
pub BlockWeights: frame_system::limits::BlockWeights =
frame_system::limits::BlockWeights::simple_max(Weight::from_parts(1024, 0));
pub const SS58Prefix: u8 = 42;
}

impl frame_system::Config for Test {
type BaseCallFilter = Everything;
type Block = Block;
type BlockWeights = ();
type BlockLength = ();
type DbWeight = ();
Expand Down Expand Up @@ -187,7 +189,7 @@ fn inflation_works() {
fn inflation_second_deposit() {
new_test_ext().execute_with(|| {
// Total issuance = 1_000_000_000
let initial_issuance: u64 = 1_000_000_000;
let initial_issuance = 1_000_000_000;
let _ = <Balances as Balanced<_>>::deposit(&1234, initial_issuance, Precision::Exact);
assert_eq!(Balances::free_balance(1234), initial_issuance);
MockBlockNumberProvider::set(1);
Expand All @@ -196,20 +198,20 @@ fn inflation_second_deposit() {
assert_ok!(Inflation::start_inflation(RawOrigin::Root.into(), 1));

// Next inflation deposit happens when block is greater then or equal to NextInflationBlock
let mut block: u64 = 2;
let balance_before: u64 = Balances::free_balance(1234);
let mut block = 2;
let balance_before = Balances::free_balance(1234);
while block < <pallet_inflation::NextInflationBlock<Test>>::get() {
MockBlockNumberProvider::set(block as u64);
MockBlockNumberProvider::set(block);
Inflation::on_initialize(0);
block += 1;
}
let balance_just_before: u64 = Balances::free_balance(1234);
let balance_just_before = Balances::free_balance(1234);
assert_eq!(balance_before, balance_just_before);

// The block with inflation
MockBlockNumberProvider::set(block as u64);
MockBlockNumberProvider::set(block);
Inflation::on_initialize(0);
let balance_after: u64 = Balances::free_balance(1234);
let balance_after = Balances::free_balance(1234);
assert_eq!(balance_after - balance_just_before, block_inflation!());
});
}
Expand All @@ -234,7 +236,7 @@ fn inflation_in_1_year() {
Inflation::on_initialize(0);
}
assert_eq!(
initial_issuance + (FIRST_YEAR_BLOCK_INFLATION * (YEAR / 100)),
initial_issuance + (FIRST_YEAR_BLOCK_INFLATION * ((YEAR as u64) / 100)),
<Balances as Inspect<_>>::total_issuance()
);

Expand All @@ -243,8 +245,8 @@ fn inflation_in_1_year() {
let block_inflation_year_2 = block_inflation!();
// Expected 100-block inflation for year 2: 100 * 9.33% * initial issuance * 110% / YEAR == 1951
let expecter_year_2_inflation: u64 = (initial_issuance
+ FIRST_YEAR_BLOCK_INFLATION * YEAR / 100)
* 933 * 100 / (10000 * YEAR);
+ FIRST_YEAR_BLOCK_INFLATION * (YEAR as u64) / 100)
* 933 * 100 / (10000 * (YEAR as u64));
assert_eq!(block_inflation_year_2 / 10, expecter_year_2_inflation / 10); // divide by 10 for approx. equality
});
}
Expand All @@ -253,8 +255,8 @@ fn inflation_in_1_year() {
fn inflation_start_large_kusama_block() {
new_test_ext().execute_with(|| {
// Total issuance = 1_000_000_000
let initial_issuance: u64 = 1_000_000_000;
let start_block: u64 = 10457457;
let initial_issuance = 1_000_000_000;
let start_block = 10457457;
let _ = <Balances as Balanced<_>>::deposit(&1234, initial_issuance, Precision::Exact);
assert_eq!(Balances::free_balance(1234), initial_issuance);
MockBlockNumberProvider::set(start_block);
Expand All @@ -273,7 +275,7 @@ fn inflation_start_large_kusama_block() {
Inflation::on_initialize(0);
}
assert_eq!(
initial_issuance + (FIRST_YEAR_BLOCK_INFLATION * (YEAR / 100)),
initial_issuance + (FIRST_YEAR_BLOCK_INFLATION * ((YEAR as u64) / 100)),
<Balances as Inspect<_>>::total_issuance()
);

Expand All @@ -282,8 +284,8 @@ fn inflation_start_large_kusama_block() {
let block_inflation_year_2 = block_inflation!();
// Expected 100-block inflation for year 2: 100 * 9.33% * initial issuance * 110% / YEAR == 1951
let expecter_year_2_inflation: u64 = (initial_issuance
+ FIRST_YEAR_BLOCK_INFLATION * YEAR / 100)
* 933 * 100 / (10000 * YEAR);
+ FIRST_YEAR_BLOCK_INFLATION * (YEAR as u64) / 100)
* 933 * 100 / (10000 * (YEAR as u64));
assert_eq!(block_inflation_year_2 / 10, expecter_year_2_inflation / 10); // divide by 10 for approx. equality
});
}
Expand Down Expand Up @@ -320,14 +322,14 @@ fn inflation_after_year_10_is_flat() {
#[test]
fn inflation_rate_by_year() {
new_test_ext().execute_with(|| {
let payouts: u64 = YEAR / InflationBlockInterval::get() as u64;
let payouts = (YEAR / InflationBlockInterval::get()) as u64;

// Inflation starts at 10% and does down by 2/3% every year until year 9 (included),
// then it is flat.
let payout_by_year: [u64; 11] = [1000, 933, 867, 800, 733, 667, 600, 533, 467, 400, 400];

// For accuracy total issuance = payout0 * payouts * 10;
let initial_issuance: u64 = payout_by_year[0] * payouts * 10;
let initial_issuance = payout_by_year[0] * payouts * 10;
let _ = <Balances as Balanced<_>>::deposit(&1234, initial_issuance, Precision::Exact);
assert_eq!(Balances::free_balance(1234), initial_issuance);

Expand Down
10 changes: 5 additions & 5 deletions runtime/common/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub use sp_runtime::AccountId32 as AccountId;
use sp_runtime::{BuildStorage, Storage};
use up_common::types::AuraId;

use crate::{BuildGenesisConfig, ParachainInfoConfig, Runtime, RuntimeEvent, System};
use crate::{ParachainInfoConfig, Runtime, RuntimeEvent, RuntimeGenesisConfig, System};
pub type Balance = u128;

pub mod xcm;
Expand Down Expand Up @@ -51,8 +51,8 @@ fn last_events(n: usize) -> Vec<RuntimeEvent> {
fn new_test_ext(balances: Vec<(AccountId, Balance)>) -> sp_io::TestExternalities {
let mut storage = make_basic_storage();

pallet_balances::BuildGenesisConfig::<Runtime> { balances }
.build_storage(&mut storage)
pallet_balances::GenesisConfig::<Runtime> { balances }
.assimilate_storage(&mut storage)
.unwrap();

let mut ext = sp_io::TestExternalities::new(storage);
Expand Down Expand Up @@ -95,7 +95,7 @@ fn make_basic_storage() -> Storage {
.map(|acc| get_account_id_from_seed::<sr25519::Public>(acc))
.collect::<Vec<_>>();

let cfg = BuildGenesisConfig {
let cfg = RuntimeGenesisConfig {
collator_selection: CollatorSelectionConfig { invulnerables },
session: SessionConfig { keys },
parachain_info: ParachainInfoConfig {
Expand All @@ -112,7 +112,7 @@ fn make_basic_storage() -> Storage {
fn make_basic_storage() -> Storage {
use crate::AuraConfig;

let cfg = BuildGenesisConfig {
let cfg = RuntimeGenesisConfig {
aura: AuraConfig {
authorities: vec![
get_from_seed::<AuraId>("Alice"),
Expand Down
6 changes: 3 additions & 3 deletions runtime/common/tests/xcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ pub fn xcm_transact_is_forbidden() {

let xcm_event = &last_events(1)[0];
match xcm_event {
RuntimeEvent::PolkadotXcm(pallet_xcm::Event::<Runtime>::Attempted(
Outcome::Incomplete(_weight, Error::NoPermission),
)) => { /* Pass */ }
RuntimeEvent::PolkadotXcm(pallet_xcm::Event::<Runtime>::Attempted {
outcome: Outcome::Incomplete(_weight, Error::NoPermission),
}) => { /* Pass */ }
_ => panic!(
"Expected PolkadotXcm.Attempted(Incomplete(_weight, NoPermission)),\
found: {xcm_event:#?}"
Expand Down
Loading

0 comments on commit c3c95ca

Please sign in to comment.