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 parity_scale_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#[pallet]38mod pallet {39 use super::*;40 use frame_support::{41 traits::{Get, ReservableCurrency, Currency},42 pallet_prelude::{StorageValue, ValueQuery, DispatchResult, IsType, OptionQuery},43 BoundedVec, log,44 };45 use frame_system::{pallet_prelude::OriginFor, ensure_root, Config as SystemConfig};46 use xcm::v1::MultiLocation;4748 pub type BalanceOf<T> =49 <<T as Config>::Currency as Currency<<T as SystemConfig>::AccountId>>::Balance;5051 #[pallet::config]52 pub trait Config: frame_system::Config {53 54 type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;5556 57 type Currency: ReservableCurrency<Self::AccountId>;5859 #[pallet::constant]60 type DefaultWeightToFeeCoefficient: Get<u64>;61 #[pallet::constant]62 type DefaultMinGasPrice: Get<u64>;6364 #[pallet::constant]65 type MaxXcmAllowedLocations: Get<u32>;66 #[pallet::constant]67 type AppPromotionDailyRate: Get<Perbill>;68 #[pallet::constant]69 type DayRelayBlocks: Get<Self::BlockNumber>;7071 #[pallet::constant]72 type DefaultCollatorSelectionMaxCollators: Get<u32>;73 #[pallet::constant]74 type DefaultCollatorSelectionLicenseBond: Get<BalanceOf<Self>>;75 #[pallet::constant]76 type DefaultCollatorSelectionKickThreshold: Get<Self::BlockNumber>;77 }7879 #[pallet::event]80 #[pallet::generate_deposit(pub(super) fn deposit_event)]81 pub enum Event<T: Config> {82 NewDesiredCollators {83 desired_collators: Option<u32>,84 },85 NewCollatorLicenseBond {86 bond_cost: Option<BalanceOf<T>>,87 },88 NewCollatorKickThreshold {89 length_in_blocks: Option<T::BlockNumber>,90 },91 }9293 #[pallet::error]94 pub enum Error<T> {95 InconsistentConfiguration,96 }9798 #[pallet::storage]99 pub type WeightToFeeCoefficientOverride<T: Config> = StorageValue<100 Value = u64,101 QueryKind = ValueQuery,102 OnEmpty = T::DefaultWeightToFeeCoefficient,103 >;104105 #[pallet::storage]106 pub type MinGasPriceOverride<T: Config> =107 StorageValue<Value = u64, QueryKind = ValueQuery, OnEmpty = T::DefaultMinGasPrice>;108109 #[pallet::storage]110 pub type XcmAllowedLocationsOverride<T: Config> = StorageValue<111 Value = BoundedVec<MultiLocation, T::MaxXcmAllowedLocations>,112 QueryKind = OptionQuery,113 >;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::DbWeight::get().writes(1))]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::DbWeight::get().writes(1))]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(2)]173 #[pallet::weight(T::DbWeight::get().writes(1))]174 pub fn set_xcm_allowed_locations(175 origin: OriginFor<T>,176 locations: Option<BoundedVec<MultiLocation, T::MaxXcmAllowedLocations>>,177 ) -> DispatchResult {178 ensure_root(origin)?;179 <XcmAllowedLocationsOverride<T>>::set(locations);180 Ok(())181 }182183 #[pallet::call_index(3)]184 #[pallet::weight(T::DbWeight::get().writes(1))]185 pub fn set_app_promotion_configuration_override(186 origin: OriginFor<T>,187 mut configuration: AppPromotionConfiguration<T::BlockNumber>,188 ) -> DispatchResult {189 ensure_root(origin)?;190 if configuration.interval_income.is_some() {191 return Err(<Error<T>>::InconsistentConfiguration.into());192 }193194 configuration.interval_income = configuration.recalculation_interval.map(|b| {195 Perbill::from_rational(b, T::DayRelayBlocks::get())196 * T::AppPromotionDailyRate::get()197 });198199 <AppPromomotionConfigurationOverride<T>>::set(configuration);200201 Ok(())202 }203204 #[pallet::call_index(4)]205 #[pallet::weight(T::DbWeight::get().writes(1))]206 pub fn set_collator_selection_desired_collators(207 origin: OriginFor<T>,208 max: Option<u32>,209 ) -> DispatchResult {210 ensure_root(origin)?;211 if let Some(max) = max {212 213 if max > T::DefaultCollatorSelectionMaxCollators::get() {214 log::warn!("max > T::DefaultCollatorSelectionMaxCollators; you might need to run benchmarks again");215 }216 <CollatorSelectionDesiredCollatorsOverride<T>>::set(max);217 } else {218 <CollatorSelectionDesiredCollatorsOverride<T>>::kill();219 }220 Self::deposit_event(Event::NewDesiredCollators {221 desired_collators: max,222 });223 Ok(())224 }225226 #[pallet::call_index(5)]227 #[pallet::weight(T::DbWeight::get().writes(1))]228 pub fn set_collator_selection_license_bond(229 origin: OriginFor<T>,230 amount: Option<BalanceOf<T>>,231 ) -> DispatchResult {232 ensure_root(origin)?;233 if let Some(amount) = amount {234 <CollatorSelectionLicenseBondOverride<T>>::set(amount);235 } else {236 <CollatorSelectionLicenseBondOverride<T>>::kill();237 }238 Self::deposit_event(Event::NewCollatorLicenseBond { bond_cost: amount });239 Ok(())240 }241242 #[pallet::call_index(6)]243 #[pallet::weight(T::DbWeight::get().writes(1))]244 pub fn set_collator_selection_kick_threshold(245 origin: OriginFor<T>,246 threshold: Option<T::BlockNumber>,247 ) -> DispatchResult {248 ensure_root(origin)?;249 if let Some(threshold) = threshold {250 <CollatorSelectionKickThresholdOverride<T>>::set(threshold);251 } else {252 <CollatorSelectionKickThresholdOverride<T>>::kill();253 }254 Self::deposit_event(Event::NewCollatorKickThreshold {255 length_in_blocks: threshold,256 });257 Ok(())258 }259 }260261 #[pallet::pallet]262 #[pallet::generate_store(pub(super) trait Store)]263 pub struct Pallet<T>(_);264}265266pub struct WeightToFee<T, B>(PhantomData<(T, B)>);267268impl<T, B> WeightToFeePolynomial for WeightToFee<T, B>269where270 T: Config,271 B: BaseArithmetic + From<u32> + From<u64> + Copy + Unsigned,272{273 type Balance = B;274275 fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {276 smallvec!(WeightToFeeCoefficient {277 coeff_integer: (<WeightToFeeCoefficientOverride<T>>::get() / Perbill::ACCURACY as u64)278 .into(),279 coeff_frac: Perbill::from_parts(280 (<WeightToFeeCoefficientOverride<T>>::get() % Perbill::ACCURACY as u64) as u32281 ),282 negative: false,283 degree: 1,284 })285 }286}287288pub struct FeeCalculator<T>(PhantomData<T>);289impl<T: Config> fp_evm::FeeCalculator for FeeCalculator<T> {290 fn min_gas_price() -> (U256, Weight) {291 (292 <MinGasPriceOverride<T>>::get().into(),293 T::DbWeight::get().reads(1),294 )295 }296}297298#[derive(Encode, Decode, Clone, Debug, Default, TypeInfo, MaxEncodedLen, PartialEq, PartialOrd)]299pub struct AppPromotionConfiguration<BlockNumber> {300 301 pub recalculation_interval: Option<BlockNumber>,302 303 pub pending_interval: Option<BlockNumber>,304 305 pub interval_income: Option<Perbill>,306 307 pub max_stakers_per_calculation: Option<u8>,308}