1234567891011121314151617#![cfg_attr(not(feature = "std"), no_std)]1819use core::marker::PhantomData;2021use frame_support::{22 pallet,23 weights::{WeightToFeePolynomial, WeightToFeeCoefficients, WeightToFeeCoefficient, Weight},24 traits::Get,25};26use codec::{Decode, Encode, MaxEncodedLen};27use scale_info::TypeInfo;28use sp_arithmetic::{29 per_things::{Perbill, PerThing},30 traits::{BaseArithmetic, Unsigned},31};32use smallvec::smallvec;3334pub use pallet::*;35use sp_core::U256;3637#[cfg(feature = "runtime-benchmarks")]38mod benchmarking;39pub mod weights;4041#[pallet]42mod pallet {43 use super::*;44 use frame_support::{45 traits::{Get, ReservableCurrency, Currency},46 pallet_prelude::{StorageValue, ValueQuery, DispatchResult, IsType},47 log,48 };49 use frame_system::{pallet_prelude::OriginFor, ensure_root, Config as SystemConfig};5051 pub use crate::weights::WeightInfo;52 pub type BalanceOf<T> =53 <<T as Config>::Currency as Currency<<T as SystemConfig>::AccountId>>::Balance;5455 #[pallet::config]56 pub trait Config: frame_system::Config {57 58 type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;5960 61 type Currency: ReservableCurrency<Self::AccountId>;6263 #[pallet::constant]64 type DefaultWeightToFeeCoefficient: Get<u64>;65 #[pallet::constant]66 type DefaultMinGasPrice: Get<u64>;6768 #[pallet::constant]69 type MaxXcmAllowedLocations: Get<u32>;70 #[pallet::constant]71 type AppPromotionDailyRate: Get<Perbill>;72 #[pallet::constant]73 type DayRelayBlocks: Get<Self::BlockNumber>;7475 #[pallet::constant]76 type DefaultCollatorSelectionMaxCollators: Get<u32>;77 #[pallet::constant]78 type DefaultCollatorSelectionLicenseBond: Get<BalanceOf<Self>>;79 #[pallet::constant]80 type DefaultCollatorSelectionKickThreshold: Get<Self::BlockNumber>;8182 83 type WeightInfo: WeightInfo;84 }8586 #[pallet::event]87 #[pallet::generate_deposit(pub(super) fn deposit_event)]88 pub enum Event<T: Config> {89 NewDesiredCollators {90 desired_collators: Option<u32>,91 },92 NewCollatorLicenseBond {93 bond_cost: Option<BalanceOf<T>>,94 },95 NewCollatorKickThreshold {96 length_in_blocks: Option<T::BlockNumber>,97 },98 }99100 #[pallet::error]101 pub enum Error<T> {102 InconsistentConfiguration,103 }104105 #[pallet::storage]106 pub type WeightToFeeCoefficientOverride<T: Config> = StorageValue<107 Value = u64,108 QueryKind = ValueQuery,109 OnEmpty = T::DefaultWeightToFeeCoefficient,110 >;111112 #[pallet::storage]113 pub type MinGasPriceOverride<T: Config> =114 StorageValue<Value = u64, QueryKind = ValueQuery, OnEmpty = T::DefaultMinGasPrice>;115116 #[pallet::storage]117 pub type AppPromomotionConfigurationOverride<T: Config> =118 StorageValue<Value = AppPromotionConfiguration<T::BlockNumber>, QueryKind = ValueQuery>;119120 #[pallet::storage]121 pub type CollatorSelectionDesiredCollatorsOverride<T: Config> = StorageValue<122 Value = u32,123 QueryKind = ValueQuery,124 OnEmpty = T::DefaultCollatorSelectionMaxCollators,125 >;126127 #[pallet::storage]128 pub type CollatorSelectionLicenseBondOverride<T: Config> = StorageValue<129 Value = BalanceOf<T>,130 QueryKind = ValueQuery,131 OnEmpty = T::DefaultCollatorSelectionLicenseBond,132 >;133134 #[pallet::storage]135 pub type CollatorSelectionKickThresholdOverride<T: Config> = StorageValue<136 Value = T::BlockNumber,137 QueryKind = ValueQuery,138 OnEmpty = T::DefaultCollatorSelectionKickThreshold,139 >;140141 #[pallet::call]142 impl<T: Config> Pallet<T> {143 #[pallet::call_index(0)]144 #[pallet::weight(T::WeightInfo::set_weight_to_fee_coefficient_override())]145 pub fn set_weight_to_fee_coefficient_override(146 origin: OriginFor<T>,147 coeff: Option<u64>,148 ) -> DispatchResult {149 ensure_root(origin)?;150 if let Some(coeff) = coeff {151 <WeightToFeeCoefficientOverride<T>>::set(coeff);152 } else {153 <WeightToFeeCoefficientOverride<T>>::kill();154 }155 Ok(())156 }157158 #[pallet::call_index(1)]159 #[pallet::weight(T::WeightInfo::set_min_gas_price_override())]160 pub fn set_min_gas_price_override(161 origin: OriginFor<T>,162 coeff: Option<u64>,163 ) -> DispatchResult {164 ensure_root(origin)?;165 if let Some(coeff) = coeff {166 <MinGasPriceOverride<T>>::set(coeff);167 } else {168 <MinGasPriceOverride<T>>::kill();169 }170 Ok(())171 }172173 #[pallet::call_index(3)]174 #[pallet::weight(T::WeightInfo::set_app_promotion_configuration_override())]175 pub fn set_app_promotion_configuration_override(176 origin: OriginFor<T>,177 mut configuration: AppPromotionConfiguration<T::BlockNumber>,178 ) -> DispatchResult {179 ensure_root(origin)?;180 if configuration.interval_income.is_some() {181 return Err(<Error<T>>::InconsistentConfiguration.into());182 }183184 configuration.interval_income = configuration.recalculation_interval.map(|b| {185 Perbill::from_rational(b, T::DayRelayBlocks::get())186 * T::AppPromotionDailyRate::get()187 });188189 <AppPromomotionConfigurationOverride<T>>::set(configuration);190191 Ok(())192 }193194 #[pallet::call_index(4)]195 #[pallet::weight(T::WeightInfo::set_collator_selection_desired_collators())]196 pub fn set_collator_selection_desired_collators(197 origin: OriginFor<T>,198 max: Option<u32>,199 ) -> DispatchResult {200 ensure_root(origin)?;201 if let Some(max) = max {202 203 if max > T::DefaultCollatorSelectionMaxCollators::get() {204 log::warn!("max > T::DefaultCollatorSelectionMaxCollators; you might need to run benchmarks again");205 }206 <CollatorSelectionDesiredCollatorsOverride<T>>::set(max);207 } else {208 <CollatorSelectionDesiredCollatorsOverride<T>>::kill();209 }210 Self::deposit_event(Event::NewDesiredCollators {211 desired_collators: max,212 });213 Ok(())214 }215216 #[pallet::call_index(5)]217 #[pallet::weight(T::WeightInfo::set_collator_selection_license_bond())]218 pub fn set_collator_selection_license_bond(219 origin: OriginFor<T>,220 amount: Option<BalanceOf<T>>,221 ) -> DispatchResult {222 ensure_root(origin)?;223 if let Some(amount) = amount {224 <CollatorSelectionLicenseBondOverride<T>>::set(amount);225 } else {226 <CollatorSelectionLicenseBondOverride<T>>::kill();227 }228 Self::deposit_event(Event::NewCollatorLicenseBond { bond_cost: amount });229 Ok(())230 }231232 #[pallet::call_index(6)]233 #[pallet::weight(T::WeightInfo::set_collator_selection_kick_threshold())]234 pub fn set_collator_selection_kick_threshold(235 origin: OriginFor<T>,236 threshold: Option<T::BlockNumber>,237 ) -> DispatchResult {238 ensure_root(origin)?;239 if let Some(threshold) = threshold {240 <CollatorSelectionKickThresholdOverride<T>>::set(threshold);241 } else {242 <CollatorSelectionKickThresholdOverride<T>>::kill();243 }244 Self::deposit_event(Event::NewCollatorKickThreshold {245 length_in_blocks: threshold,246 });247 Ok(())248 }249 }250251 #[pallet::pallet]252 #[pallet::generate_store(pub(super) trait Store)]253 pub struct Pallet<T>(_);254}255256pub struct WeightToFee<T, B>(PhantomData<(T, B)>);257258impl<T, B> WeightToFeePolynomial for WeightToFee<T, B>259where260 T: Config,261 B: BaseArithmetic + From<u32> + From<u64> + Copy + Unsigned,262{263 type Balance = B;264265 fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {266 smallvec!(WeightToFeeCoefficient {267 coeff_integer: (<WeightToFeeCoefficientOverride<T>>::get() / Perbill::ACCURACY as u64)268 .into(),269 coeff_frac: Perbill::from_parts(270 (<WeightToFeeCoefficientOverride<T>>::get() % Perbill::ACCURACY as u64) as u32271 ),272 negative: false,273 degree: 1,274 })275 }276}277278pub struct FeeCalculator<T>(PhantomData<T>);279impl<T: Config> fp_evm::FeeCalculator for FeeCalculator<T> {280 fn min_gas_price() -> (U256, Weight) {281 (282 <MinGasPriceOverride<T>>::get().into(),283 T::DbWeight::get().reads(1),284 )285 }286}287288#[derive(Encode, Decode, Clone, Debug, Default, TypeInfo, MaxEncodedLen, PartialEq, PartialOrd)]289pub struct AppPromotionConfiguration<BlockNumber> {290 291 pub recalculation_interval: Option<BlockNumber>,292 293 pub pending_interval: Option<BlockNumber>,294 295 pub interval_income: Option<Perbill>,296 297 pub max_stakers_per_calculation: Option<u8>,298}