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}, log,47 };48 use frame_system::{pallet_prelude::OriginFor, ensure_root, Config as SystemConfig};4950 pub use crate::weights::WeightInfo;51 pub type BalanceOf<T> =52 <<T as Config>::Currency as Currency<<T as SystemConfig>::AccountId>>::Balance;5354 #[pallet::config]55 pub trait Config: frame_system::Config {56 57 type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;5859 60 type Currency: ReservableCurrency<Self::AccountId>;6162 #[pallet::constant]63 type DefaultWeightToFeeCoefficient: Get<u64>;64 #[pallet::constant]65 type DefaultMinGasPrice: Get<u64>;6667 #[pallet::constant]68 type MaxXcmAllowedLocations: Get<u32>;69 #[pallet::constant]70 type AppPromotionDailyRate: Get<Perbill>;71 #[pallet::constant]72 type DayRelayBlocks: Get<Self::BlockNumber>;7374 #[pallet::constant]75 type DefaultCollatorSelectionMaxCollators: Get<u32>;76 #[pallet::constant]77 type DefaultCollatorSelectionLicenseBond: Get<BalanceOf<Self>>;78 #[pallet::constant]79 type DefaultCollatorSelectionKickThreshold: Get<Self::BlockNumber>;8081 82 type WeightInfo: WeightInfo;83 }8485 #[pallet::event]86 #[pallet::generate_deposit(pub(super) fn deposit_event)]87 pub enum Event<T: Config> {88 NewDesiredCollators {89 desired_collators: Option<u32>,90 },91 NewCollatorLicenseBond {92 bond_cost: Option<BalanceOf<T>>,93 },94 NewCollatorKickThreshold {95 length_in_blocks: Option<T::BlockNumber>,96 },97 }9899 #[pallet::error]100 pub enum Error<T> {101 InconsistentConfiguration,102 }103104 #[pallet::storage]105 pub type WeightToFeeCoefficientOverride<T: Config> = StorageValue<106 Value = u64,107 QueryKind = ValueQuery,108 OnEmpty = T::DefaultWeightToFeeCoefficient,109 >;110111 #[pallet::storage]112 pub type MinGasPriceOverride<T: Config> =113 StorageValue<Value = u64, QueryKind = ValueQuery, OnEmpty = T::DefaultMinGasPrice>;114115 #[pallet::storage]116 pub type AppPromomotionConfigurationOverride<T: Config> =117 StorageValue<Value = AppPromotionConfiguration<T::BlockNumber>, QueryKind = ValueQuery>;118119 #[pallet::storage]120 pub type CollatorSelectionDesiredCollatorsOverride<T: Config> = StorageValue<121 Value = u32,122 QueryKind = ValueQuery,123 OnEmpty = T::DefaultCollatorSelectionMaxCollators,124 >;125126 #[pallet::storage]127 pub type CollatorSelectionLicenseBondOverride<T: Config> = StorageValue<128 Value = BalanceOf<T>,129 QueryKind = ValueQuery,130 OnEmpty = T::DefaultCollatorSelectionLicenseBond,131 >;132133 #[pallet::storage]134 pub type CollatorSelectionKickThresholdOverride<T: Config> = StorageValue<135 Value = T::BlockNumber,136 QueryKind = ValueQuery,137 OnEmpty = T::DefaultCollatorSelectionKickThreshold,138 >;139140 #[pallet::call]141 impl<T: Config> Pallet<T> {142 #[pallet::call_index(0)]143 #[pallet::weight(T::WeightInfo::set_weight_to_fee_coefficient_override())]144 pub fn set_weight_to_fee_coefficient_override(145 origin: OriginFor<T>,146 coeff: Option<u64>,147 ) -> DispatchResult {148 ensure_root(origin)?;149 if let Some(coeff) = coeff {150 <WeightToFeeCoefficientOverride<T>>::set(coeff);151 } else {152 <WeightToFeeCoefficientOverride<T>>::kill();153 }154 Ok(())155 }156157 #[pallet::call_index(1)]158 #[pallet::weight(T::WeightInfo::set_min_gas_price_override())]159 pub fn set_min_gas_price_override(160 origin: OriginFor<T>,161 coeff: Option<u64>,162 ) -> DispatchResult {163 ensure_root(origin)?;164 if let Some(coeff) = coeff {165 <MinGasPriceOverride<T>>::set(coeff);166 } else {167 <MinGasPriceOverride<T>>::kill();168 }169 Ok(())170 }171172 #[pallet::call_index(3)]173 #[pallet::weight(T::WeightInfo::set_app_promotion_configuration_override())]174 pub fn set_app_promotion_configuration_override(175 origin: OriginFor<T>,176 mut configuration: AppPromotionConfiguration<T::BlockNumber>,177 ) -> DispatchResult {178 ensure_root(origin)?;179 if configuration.interval_income.is_some() {180 return Err(<Error<T>>::InconsistentConfiguration.into());181 }182183 configuration.interval_income = configuration.recalculation_interval.map(|b| {184 Perbill::from_rational(b, T::DayRelayBlocks::get())185 * T::AppPromotionDailyRate::get()186 });187188 <AppPromomotionConfigurationOverride<T>>::set(configuration);189190 Ok(())191 }192193 #[pallet::call_index(4)]194 #[pallet::weight(T::WeightInfo::set_collator_selection_desired_collators())]195 pub fn set_collator_selection_desired_collators(196 origin: OriginFor<T>,197 max: Option<u32>,198 ) -> DispatchResult {199 ensure_root(origin)?;200 if let Some(max) = max {201 202 if max > T::DefaultCollatorSelectionMaxCollators::get() {203 log::warn!("max > T::DefaultCollatorSelectionMaxCollators; you might need to run benchmarks again");204 }205 <CollatorSelectionDesiredCollatorsOverride<T>>::set(max);206 } else {207 <CollatorSelectionDesiredCollatorsOverride<T>>::kill();208 }209 Self::deposit_event(Event::NewDesiredCollators {210 desired_collators: max,211 });212 Ok(())213 }214215 #[pallet::call_index(5)]216 #[pallet::weight(T::WeightInfo::set_collator_selection_license_bond())]217 pub fn set_collator_selection_license_bond(218 origin: OriginFor<T>,219 amount: Option<BalanceOf<T>>,220 ) -> DispatchResult {221 ensure_root(origin)?;222 if let Some(amount) = amount {223 <CollatorSelectionLicenseBondOverride<T>>::set(amount);224 } else {225 <CollatorSelectionLicenseBondOverride<T>>::kill();226 }227 Self::deposit_event(Event::NewCollatorLicenseBond { bond_cost: amount });228 Ok(())229 }230231 #[pallet::call_index(6)]232 #[pallet::weight(T::WeightInfo::set_collator_selection_kick_threshold())]233 pub fn set_collator_selection_kick_threshold(234 origin: OriginFor<T>,235 threshold: Option<T::BlockNumber>,236 ) -> DispatchResult {237 ensure_root(origin)?;238 if let Some(threshold) = threshold {239 <CollatorSelectionKickThresholdOverride<T>>::set(threshold);240 } else {241 <CollatorSelectionKickThresholdOverride<T>>::kill();242 }243 Self::deposit_event(Event::NewCollatorKickThreshold {244 length_in_blocks: threshold,245 });246 Ok(())247 }248 }249250 #[pallet::pallet]251 #[pallet::generate_store(pub(super) trait Store)]252 pub struct Pallet<T>(_);253}254255pub struct WeightToFee<T, B>(PhantomData<(T, B)>);256257impl<T, B> WeightToFeePolynomial for WeightToFee<T, B>258where259 T: Config,260 B: BaseArithmetic + From<u32> + From<u64> + Copy + Unsigned,261{262 type Balance = B;263264 fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {265 smallvec!(WeightToFeeCoefficient {266 coeff_integer: (<WeightToFeeCoefficientOverride<T>>::get() / Perbill::ACCURACY as u64)267 .into(),268 coeff_frac: Perbill::from_parts(269 (<WeightToFeeCoefficientOverride<T>>::get() % Perbill::ACCURACY as u64) as u32270 ),271 negative: false,272 degree: 1,273 })274 }275}276277pub struct FeeCalculator<T>(PhantomData<T>);278impl<T: Config> fp_evm::FeeCalculator for FeeCalculator<T> {279 fn min_gas_price() -> (U256, Weight) {280 (281 <MinGasPriceOverride<T>>::get().into(),282 T::DbWeight::get().reads(1),283 )284 }285}286287#[derive(Encode, Decode, Clone, Debug, Default, TypeInfo, MaxEncodedLen, PartialEq, PartialOrd)]288pub struct AppPromotionConfiguration<BlockNumber> {289 290 pub recalculation_interval: Option<BlockNumber>,291 292 pub pending_interval: Option<BlockNumber>,293 294 pub interval_income: Option<Perbill>,295 296 pub max_stakers_per_calculation: Option<u8>,297}