Lines
96.72 %
Functions
26.51 %
Branches
100 %
// Copyright 2019-2025 PureStake Inc.
// This file is part of Moonbeam.
// Moonbeam is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Moonbeam is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Moonbeam. If not, see <http://www.gnu.org/licenses/>.
//! Test utilities
use super::*;
use frame_support::{
construct_runtime, parameter_types,
traits::{Everything, Get, OnFinalize, OnInitialize},
weights::Weight,
};
use frame_system::pallet_prelude::BlockNumberFor;
use pallet_evm::{EnsureAddressNever, EnsureAddressRoot, FrameSystemAccountProvider};
use pallet_parachain_staking::{AwardedPts, InflationInfo, Points, Range};
use precompile_utils::{
precompile_set::*,
testing::{Alice, MockAccount},
use sp_consensus_slots::Slot;
use sp_core::{H256, U256};
use sp_io;
use sp_runtime::{
traits::{BlakeTwo256, IdentityLookup},
BuildStorage, Perbill, Percent,
pub type AccountId = MockAccount;
pub type Balance = u128;
pub type BlockNumber = BlockNumberFor<Runtime>;
type Block = frame_system::mocking::MockBlockU32<Runtime>;
construct_runtime!(
pub enum Runtime {
System: frame_system,
Balances: pallet_balances,
Evm: pallet_evm,
Timestamp: pallet_timestamp,
ParachainStaking: pallet_parachain_staking,
}
);
parameter_types! {
pub const BlockHashCount: u32 = 250;
pub const MaximumBlockWeight: Weight = Weight::from_parts(1024, 1);
pub const MaximumBlockLength: u32 = 2 * 1024;
pub const AvailableBlockRatio: Perbill = Perbill::one();
pub const SS58Prefix: u8 = 42;
impl frame_system::Config for Runtime {
type BaseCallFilter = Everything;
type DbWeight = ();
type RuntimeOrigin = RuntimeOrigin;
type RuntimeTask = RuntimeTask;
type Nonce = u64;
type Block = Block;
type RuntimeCall = RuntimeCall;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = AccountId;
type Lookup = IdentityLookup<AccountId>;
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = BlockHashCount;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<Balance>;
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type BlockWeights = ();
type BlockLength = ();
type SS58Prefix = SS58Prefix;
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
type SingleBlockMigrations = ();
type MultiBlockMigrator = ();
type PreInherents = ();
type PostInherents = ();
type PostTransactions = ();
type ExtensionsWeightInfo = ();
pub const ExistentialDeposit: u128 = 0;
impl pallet_balances::Config for Runtime {
type MaxReserves = ();
type ReserveIdentifier = [u8; 4];
type MaxLocks = ();
type Balance = Balance;
type DustRemoval = ();
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = System;
type WeightInfo = ();
type RuntimeHoldReason = ();
type FreezeIdentifier = ();
type MaxFreezes = ();
type RuntimeFreezeReason = ();
type DoneSlashHandler = ();
const MAX_POV_SIZE: u64 = 5 * 1024 * 1024;
/// Block storage limit in bytes. Set to 40 KB.
const BLOCK_STORAGE_LIMIT: u64 = 40 * 1024;
pub BlockGasLimit: U256 = U256::from(u64::MAX);
pub PrecompilesValue: Precompiles<Runtime> = Precompiles::new();
pub const WeightPerGas: Weight = Weight::from_parts(1, 0);
pub GasLimitPovSizeRatio: u64 = {
let block_gas_limit = BlockGasLimit::get().min(u64::MAX.into()).low_u64();
block_gas_limit.saturating_div(MAX_POV_SIZE)
pub GasLimitStorageGrowthRatio: u64 = {
block_gas_limit.saturating_div(BLOCK_STORAGE_LIMIT)
pub SuicideQuickClearLimit: u32 = 0;
pub type Precompiles<R> =
PrecompileSetBuilder<R, (PrecompileAt<AddressU64<1>, ParachainStakingPrecompile<R>>,)>;
pub type PCall = ParachainStakingPrecompileCall<Runtime>;
impl pallet_evm::Config for Runtime {
type FeeCalculator = ();
type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
type WeightPerGas = WeightPerGas;
type CallOrigin = EnsureAddressRoot<AccountId>;
type WithdrawOrigin = EnsureAddressNever<AccountId>;
type AddressMapping = AccountId;
type Currency = Balances;
type Runner = pallet_evm::runner::stack::Runner<Self>;
type PrecompilesType = Precompiles<Runtime>;
type PrecompilesValue = PrecompilesValue;
type ChainId = ();
type OnChargeTransaction = ();
type BlockGasLimit = BlockGasLimit;
type BlockHashMapping = pallet_evm::SubstrateBlockHashMapping<Self>;
type FindAuthor = ();
type OnCreate = ();
type GasLimitPovSizeRatio = GasLimitPovSizeRatio;
type GasLimitStorageGrowthRatio = GasLimitStorageGrowthRatio;
type Timestamp = Timestamp;
type WeightInfo = pallet_evm::weights::SubstrateWeight<Runtime>;
type AccountProvider = FrameSystemAccountProvider<Runtime>;
pub const MinimumPeriod: u64 = 5;
impl pallet_timestamp::Config for Runtime {
type Moment = u64;
type OnTimestampSet = ();
type MinimumPeriod = MinimumPeriod;
const GENESIS_BLOCKS_PER_ROUND: u32 = 5;
const GENESIS_COLLATOR_COMMISSION: Perbill = Perbill::from_percent(20);
const GENESIS_PARACHAIN_BOND_RESERVE_PERCENT: Percent = Percent::from_percent(30);
const GENESIS_NUM_SELECTED_CANDIDATES: u32 = 5;
pub const MinBlocksPerRound: u32 = 3;
pub const MaxOfflineRounds: u32 = 2;
pub const LeaveCandidatesDelay: u32 = 2;
pub const CandidateBondLessDelay: u32 = 2;
pub const LeaveDelegatorsDelay: u32 = 2;
pub const RevokeDelegationDelay: u32 = 2;
pub const DelegationBondLessDelay: u32 = 2;
pub const RewardPaymentDelay: u32 = 2;
pub const MinSelectedCandidates: u32 = GENESIS_NUM_SELECTED_CANDIDATES;
pub const MaxTopDelegationsPerCandidate: u32 = 2;
pub const MaxBottomDelegationsPerCandidate: u32 = 4;
pub const MaxDelegationsPerDelegator: u32 = 4;
pub const MinCandidateStk: u128 = 10;
pub const MinDelegation: u128 = 3;
pub const MaxCandidates: u32 = 10;
pub BlockAuthor: AccountId = Alice.into();
pub struct StakingRoundSlotProvider;
impl Get<Slot> for StakingRoundSlotProvider {
fn get() -> Slot {
let block_number: u64 = System::block_number().into();
Slot::from(block_number)
impl pallet_parachain_staking::Config for Runtime {
type MonetaryGovernanceOrigin = frame_system::EnsureRoot<AccountId>;
type MinBlocksPerRound = MinBlocksPerRound;
type MaxOfflineRounds = MaxOfflineRounds;
type LeaveCandidatesDelay = LeaveCandidatesDelay;
type CandidateBondLessDelay = CandidateBondLessDelay;
type LeaveDelegatorsDelay = LeaveDelegatorsDelay;
type RevokeDelegationDelay = RevokeDelegationDelay;
type DelegationBondLessDelay = DelegationBondLessDelay;
type RewardPaymentDelay = RewardPaymentDelay;
type MinSelectedCandidates = MinSelectedCandidates;
type MaxTopDelegationsPerCandidate = MaxTopDelegationsPerCandidate;
type MaxBottomDelegationsPerCandidate = MaxBottomDelegationsPerCandidate;
type MaxDelegationsPerDelegator = MaxDelegationsPerDelegator;
type MinCandidateStk = MinCandidateStk;
type MinDelegation = MinDelegation;
type BlockAuthor = BlockAuthor;
type PayoutCollatorReward = ();
type OnCollatorPayout = ();
type OnInactiveCollator = ();
type OnNewRound = ();
type SlotProvider = StakingRoundSlotProvider;
type MaxCandidates = MaxCandidates;
type SlotDuration = frame_support::traits::ConstU64<6_000>;
type BlockTime = frame_support::traits::ConstU64<6_000>;
pub(crate) struct ExtBuilder {
// endowed accounts with balances
balances: Vec<(AccountId, Balance)>,
// [collator, amount]
collators: Vec<(AccountId, Balance)>,
// [delegator, collator, delegation_amount]
delegations: Vec<(AccountId, AccountId, Balance, Percent)>,
// inflation config
inflation: InflationInfo<Balance>,
impl Default for ExtBuilder {
fn default() -> ExtBuilder {
ExtBuilder {
balances: vec![],
delegations: vec![],
collators: vec![],
inflation: InflationInfo {
expect: Range {
min: 700,
ideal: 700,
max: 700,
},
// not used
annual: Range {
min: Perbill::from_percent(50),
ideal: Perbill::from_percent(50),
max: Perbill::from_percent(50),
// unrealistically high parameterization, only for testing
round: Range {
min: Perbill::from_percent(5),
ideal: Perbill::from_percent(5),
max: Perbill::from_percent(5),
impl ExtBuilder {
pub(crate) fn with_balances(mut self, balances: Vec<(AccountId, Balance)>) -> Self {
self.balances = balances;
self
pub(crate) fn with_candidates(mut self, collators: Vec<(AccountId, Balance)>) -> Self {
self.collators = collators;
pub(crate) fn with_delegations(
mut self,
delegations: Vec<(AccountId, AccountId, Balance)>,
) -> Self {
self.delegations = delegations
.into_iter()
.map(|d| (d.0, d.1, d.2, Percent::zero()))
.collect();
pub(crate) fn with_auto_compounding_delegations(
.map(|d| (d.0, d.1, d.2, d.3))
#[allow(dead_code)]
pub(crate) fn with_inflation(mut self, inflation: InflationInfo<Balance>) -> Self {
self.inflation = inflation;
pub(crate) fn build(self) -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::<Runtime>::default()
.build_storage()
.expect("Frame system builds valid default genesis config");
pallet_balances::GenesisConfig::<Runtime> {
balances: self.balances,
.assimilate_storage(&mut t)
.expect("Pallet balances storage can be assimilated");
pallet_parachain_staking::GenesisConfig::<Runtime> {
candidates: self.collators,
delegations: self.delegations,
inflation_config: self.inflation,
collator_commission: GENESIS_COLLATOR_COMMISSION,
parachain_bond_reserve_percent: GENESIS_PARACHAIN_BOND_RESERVE_PERCENT,
blocks_per_round: GENESIS_BLOCKS_PER_ROUND,
num_selected_candidates: GENESIS_NUM_SELECTED_CANDIDATES,
.expect("Parachain Staking's storage can be assimilated");
let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| System::set_block_number(1));
ext
// Sets the same storage changes as EventHandler::note_author impl
pub(crate) fn set_points(round: BlockNumber, acc: impl Into<AccountId>, pts: u32) {
<Points<Runtime>>::mutate(round, |p| *p += pts);
<AwardedPts<Runtime>>::mutate(round, acc.into(), |p| *p += pts);
pub(crate) fn roll_to(n: BlockNumber) {
while System::block_number() < n {
ParachainStaking::on_finalize(System::block_number());
Balances::on_finalize(System::block_number());
System::on_finalize(System::block_number());
System::set_block_number(System::block_number() + 1);
System::on_initialize(System::block_number());
Balances::on_initialize(System::block_number());
ParachainStaking::on_initialize(System::block_number());
/// Rolls block-by-block to the beginning of the specified round.
/// This will complete the block in which the round change occurs.
pub(crate) fn roll_to_round_begin(round: BlockNumber) {
let block = (round - 1) * GENESIS_BLOCKS_PER_ROUND;
roll_to(block)
pub(crate) fn events() -> Vec<RuntimeEvent> {
System::events()
.map(|r| r.event)
.collect::<Vec<_>>()