git.delta.rocks / unique-network / refs/commits / aa49f021671b

difftreelog

feat use pallet-configuration

Yaroslav Bolyukin2022-08-15parent: #a7efb3e.patch.diff
in: master

11 files changed

addedpallets/configuration/Cargo.tomldiffbeforeafterboth
--- /dev/null
+++ b/pallets/configuration/Cargo.toml
@@ -0,0 +1,33 @@
+[package]
+name = "pallet-configuration"
+version = "0.1.0"
+edition = "2021"
+
+[dependencies]
+parity-scale-codec = { version = "3.1.5", features = [
+	"derive",
+], default-features = false }
+scale-info = { version = "2.0.1", default-features = false, features = [
+	"derive",
+] }
+frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24" }
+frame-system = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24" }
+sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24" }
+sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24" }
+sp-core = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24" }
+sp-arithmetic = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24" }
+fp-evm = { default-features = false, git = "https://github.com/uniquenetwork/frontier", branch = "unique-polkadot-v0.9.24" }
+smallvec = "1.6.1"
+
+[features]
+default = ["std"]
+std = [
+	"parity-scale-codec/std",
+	"frame-support/std",
+	"frame-system/std",
+	"sp-runtime/std",
+	"sp-std/std",
+	"sp-core/std",
+	"sp-arithmetic/std",
+	"fp-evm/std",
+]
addedpallets/configuration/src/lib.rsdiffbeforeafterboth
--- /dev/null
+++ b/pallets/configuration/src/lib.rs
@@ -0,0 +1,124 @@
+// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
+// This file is part of Unique Network.
+
+// Unique Network 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.
+
+// Unique Network 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 Unique Network. If not, see <http://www.gnu.org/licenses/>.
+
+#![cfg_attr(not(feature = "std"), no_std)]
+
+use core::marker::PhantomData;
+
+use frame_support::{
+	pallet,
+	weights::{WeightToFeePolynomial, WeightToFeeCoefficients, WeightToFeeCoefficient},
+	traits::Get,
+};
+use sp_arithmetic::traits::{BaseArithmetic, Unsigned};
+use smallvec::smallvec;
+
+pub use pallet::*;
+use sp_core::U256;
+use sp_runtime::Perbill;
+
+#[pallet]
+mod pallet {
+	use super::*;
+	use frame_support::{
+		traits::Get,
+		pallet_prelude::{StorageValue, ValueQuery, DispatchResult},
+	};
+	use frame_system::{pallet_prelude::OriginFor, ensure_root};
+
+	#[pallet::config]
+	pub trait Config: frame_system::Config {
+		#[pallet::constant]
+		type DefaultWeightToFeeCoefficient: Get<u32>;
+		#[pallet::constant]
+		type DefaultMinGasPrice: Get<u64>;
+	}
+
+	#[pallet::storage]
+	pub type WeightToFeeCoefficientOverride<T: Config> = StorageValue<
+		Value = u32,
+		QueryKind = ValueQuery,
+		OnEmpty = T::DefaultWeightToFeeCoefficient,
+	>;
+
+	#[pallet::storage]
+	pub type MinGasPriceOverride<T: Config> =
+		StorageValue<Value = u64, QueryKind = ValueQuery, OnEmpty = T::DefaultMinGasPrice>;
+
+	#[pallet::call]
+	impl<T: Config> Pallet<T> {
+		#[pallet::weight(T::DbWeight::get().writes(1))]
+		pub fn set_weight_to_fee_coefficient_override(
+			origin: OriginFor<T>,
+			coeff: Option<u32>,
+		) -> DispatchResult {
+			let _sender = ensure_root(origin)?;
+			if let Some(coeff) = coeff {
+				<WeightToFeeCoefficientOverride<T>>::set(coeff);
+			} else {
+				<WeightToFeeCoefficientOverride<T>>::kill();
+			}
+			Ok(())
+		}
+
+		#[pallet::weight(T::DbWeight::get().writes(1))]
+		pub fn set_min_gas_price_override(
+			origin: OriginFor<T>,
+			coeff: Option<u64>,
+		) -> DispatchResult {
+			let _sender = ensure_root(origin)?;
+			if let Some(coeff) = coeff {
+				<MinGasPriceOverride<T>>::set(coeff);
+			} else {
+				<MinGasPriceOverride<T>>::kill();
+			}
+			Ok(())
+		}
+	}
+
+	#[pallet::pallet]
+	#[pallet::generate_store(pub(super) trait Store)]
+	pub struct Pallet<T>(_);
+}
+
+pub struct WeightToFee<T, B>(PhantomData<(T, B)>);
+
+impl<T, B> WeightToFeePolynomial for WeightToFee<T, B>
+where
+	T: Config,
+	B: BaseArithmetic + From<u32> + Copy + Unsigned,
+{
+	type Balance = B;
+
+	fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
+		smallvec!(WeightToFeeCoefficient {
+			coeff_integer: <WeightToFeeCoefficientOverride<T>>::get().into(),
+			coeff_frac: Perbill::zero(),
+			negative: false,
+			degree: 1,
+		})
+	}
+}
+
+pub struct FeeCalculator<T>(PhantomData<T>);
+impl<T: Config> fp_evm::FeeCalculator for FeeCalculator<T> {
+	fn min_gas_price() -> (U256, u64) {
+		(
+			<MinGasPriceOverride<T>>::get().into(),
+			T::DbWeight::get().reads(1),
+		)
+	}
+}
modifiedprimitives/common/src/constants.rsdiffbeforeafterboth
--- a/primitives/common/src/constants.rs
+++ b/primitives/common/src/constants.rs
@@ -36,10 +36,10 @@
 pub const UNIQUE: Balance = 100 * CENTIUNIQUE;
 
 // Targeting 0.1 UNQ per transfer
-pub const WEIGHT_TO_FEE_COEFF: u32 = 207_890_902;
+pub const WEIGHT_TO_FEE_COEFF: u32 = /*<weight2fee>*/207_890_902/*</weight2fee>*/;
 
 // Targeting 0.15 UNQ per transfer via ETH
-pub const MIN_GAS_PRICE: u64 = 1_019_493_469_850;
+pub const MIN_GAS_PRICE: u64 = /*<mingasprice>*/1_019_493_469_850/*</mingasprice>*/;
 
 /// We assume that ~10% of the block weight is consumed by `on_initalize` handlers.
 /// This is used to limit the maximal weight of a single extrinsic.
modifiedruntime/common/config/ethereum.rsdiffbeforeafterboth
--- a/runtime/common/config/ethereum.rs
+++ b/runtime/common/config/ethereum.rs
@@ -50,13 +50,6 @@
 	}
 }
 
-pub struct FixedFee;
-impl pallet_evm::FeeCalculator for FixedFee {
-	fn min_gas_price() -> (U256, u64) {
-		(MIN_GAS_PRICE.into(), 0)
-	}
-}
-
 pub struct EthereumFindAuthor<F>(core::marker::PhantomData<F>);
 impl<F: FindAuthor<u32>> FindAuthor<H160> for EthereumFindAuthor<F> {
 	fn find_author<'a, I>(digests: I) -> Option<H160>
@@ -73,7 +66,7 @@
 
 impl pallet_evm::Config for Runtime {
 	type BlockGasLimit = BlockGasLimit;
-	type FeeCalculator = FixedFee;
+	type FeeCalculator = pallet_configuration::FeeCalculator<Self>;
 	type GasWeightMapping = FixedGasWeightMapping;
 	type BlockHashMapping = pallet_ethereum::EthereumBlockHashMapping<Self>;
 	type CallOrigin = EnsureAddressTruncated<Self>;
modifiedruntime/common/config/pallets/mod.rsdiffbeforeafterboth
--- a/runtime/common/config/pallets/mod.rs
+++ b/runtime/common/config/pallets/mod.rs
@@ -25,6 +25,7 @@
 	},
 	Runtime, Event, Call, Balances,
 };
+use frame_support::traits::{ConstU32, ConstU64};
 use up_common::{
 	types::{AccountId, Balance, BlockNumber},
 	constants::*,
@@ -91,3 +92,8 @@
 	type CommonWeightInfo = CommonWeights<Self>;
 	type RefungibleExtensionsWeightInfo = CommonWeights<Self>;
 }
+
+impl pallet_configuration::Config for Runtime {
+	type DefaultWeightToFeeCoefficient = ConstU32<{ up_common::constants::WEIGHT_TO_FEE_COEFF }>;
+	type DefaultMinGasPrice = ConstU64<{ up_common::constants::MIN_GAS_PRICE }>;
+}
modifiedruntime/common/config/substrate.rsdiffbeforeafterboth
--- a/runtime/common/config/substrate.rs
+++ b/runtime/common/config/substrate.rs
@@ -18,8 +18,7 @@
 	traits::{Everything, ConstU32},
 	weights::{
 		constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight},
-		DispatchClass, WeightToFeePolynomial, WeightToFeeCoefficients, ConstantMultiplier,
-		WeightToFeeCoefficient,
+		DispatchClass, ConstantMultiplier,
 	},
 	parameter_types, PalletId,
 };
@@ -32,8 +31,6 @@
 	limits::{BlockLength, BlockWeights},
 	EnsureRoot,
 };
-use sp_arithmetic::traits::{BaseArithmetic, Unsigned};
-use smallvec::smallvec;
 use crate::{
 	runtime_common::DealWithFees, Runtime, Event, Call, Origin, PalletInfo, System, Balances,
 	Treasury, SS58Prefix, Version,
@@ -154,32 +151,13 @@
 	/// This value increases the priority of `Operational` transactions by adding
 	/// a "virtual tip" that's equal to the `OperationalFeeMultiplier * final_fee`.
 	pub const OperationalFeeMultiplier: u8 = 5;
-}
-
-/// Linear implementor of `WeightToFeePolynomial`
-pub struct LinearFee<T>(sp_std::marker::PhantomData<T>);
-
-impl<T> WeightToFeePolynomial for LinearFee<T>
-where
-	T: BaseArithmetic + From<u32> + Copy + Unsigned,
-{
-	type Balance = T;
-
-	fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
-		smallvec!(WeightToFeeCoefficient {
-			coeff_integer: WEIGHT_TO_FEE_COEFF.into(),
-			coeff_frac: Perbill::zero(),
-			negative: false,
-			degree: 1,
-		})
-	}
 }
 
 impl pallet_transaction_payment::Config for Runtime {
 	type OnChargeTransaction = pallet_transaction_payment::CurrencyAdapter<Balances, DealWithFees>;
 	type LengthToFee = ConstantMultiplier<Balance, TransactionByteFee>;
 	type OperationalFeeMultiplier = OperationalFeeMultiplier;
-	type WeightToFee = LinearFee<Balance>;
+	type WeightToFee = pallet_configuration::WeightToFee<Self, Balance>;
 	type FeeMultiplierUpdate = ();
 }
 
modifiedruntime/common/config/xcm.rsdiffbeforeafterboth
--- a/runtime/common/config/xcm.rs
+++ b/runtime/common/config/xcm.rs
@@ -44,8 +44,7 @@
 use xcm_executor::{Config, XcmExecutor, Assets};
 use sp_std::marker::PhantomData;
 use crate::{
-	runtime_common::config::substrate::LinearFee, Runtime, Call, Event, Origin, Balances,
-	ParachainInfo, ParachainSystem, PolkadotXcm, XcmpQueue,
+	Runtime, Call, Event, Origin, Balances, ParachainInfo, ParachainSystem, PolkadotXcm, XcmpQueue,
 };
 use up_common::{
 	types::{AccountId, Balance},
@@ -234,8 +233,11 @@
 	}
 }
 
-pub struct XcmConfig;
-impl Config for XcmConfig {
+pub struct XcmConfig<T>(PhantomData<T>);
+impl<T> Config for XcmConfig<T>
+where
+	T: pallet_configuration::Config,
+{
 	type Call = Call;
 	type XcmSender = XcmRouter;
 	// How to withdraw and deposit an asset.
@@ -246,8 +248,13 @@
 	type LocationInverter = LocationInverter<Ancestry>;
 	type Barrier = Barrier;
 	type Weigher = FixedWeightBounds<UnitWeightCost, Call, MaxInstructions>;
-	type Trader =
-		UsingOnlySelfCurrencyComponents<LinearFee<Balance>, RelayLocation, AccountId, Balances, ()>;
+	type Trader = UsingOnlySelfCurrencyComponents<
+		pallet_configuration::WeightToFee<T, Balance>,
+		RelayLocation,
+		AccountId,
+		Balances,
+		(),
+	>;
 	type ResponseHandler = (); // Don't handle responses for now.
 	type SubscriptionService = PolkadotXcm;
 
@@ -261,7 +268,7 @@
 	type XcmRouter = XcmRouter;
 	type ExecuteXcmOrigin = EnsureXcmOrigin<Origin, LocalOriginToLocation>;
 	type XcmExecuteFilter = Everything;
-	type XcmExecutor = XcmExecutor<XcmConfig>;
+	type XcmExecutor = XcmExecutor<XcmConfig<Self>>;
 	type XcmTeleportFilter = Everything;
 	type XcmReserveTransferFilter = Everything;
 	type Weigher = FixedWeightBounds<UnitWeightCost, Call, MaxInstructions>;
@@ -274,13 +281,13 @@
 
 impl cumulus_pallet_xcm::Config for Runtime {
 	type Event = Event;
-	type XcmExecutor = XcmExecutor<XcmConfig>;
+	type XcmExecutor = XcmExecutor<XcmConfig<Self>>;
 }
 
 impl cumulus_pallet_xcmp_queue::Config for Runtime {
 	type WeightInfo = ();
 	type Event = Event;
-	type XcmExecutor = XcmExecutor<XcmConfig>;
+	type XcmExecutor = XcmExecutor<XcmConfig<Self>>;
 	type ChannelInfo = ParachainSystem;
 	type VersionWrapper = ();
 	type ExecuteOverweightOrigin = frame_system::EnsureRoot<AccountId>;
@@ -290,6 +297,6 @@
 
 impl cumulus_pallet_dmp_queue::Config for Runtime {
 	type Event = Event;
-	type XcmExecutor = XcmExecutor<XcmConfig>;
+	type XcmExecutor = XcmExecutor<XcmConfig<Self>>;
 	type ExecuteOverweightOrigin = frame_system::EnsureRoot<AccountId>;
 }
modifiedruntime/common/construct_runtime/mod.rsdiffbeforeafterboth
before · runtime/common/construct_runtime/mod.rs
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617mod util;1819#[macro_export]20macro_rules! construct_runtime {21    ($select_runtime:ident) => {22        $crate::construct_runtime_impl! {23            select_runtime($select_runtime);2425            pub enum Runtime where26                Block = Block,27                NodeBlock = opaque::Block,28                UncheckedExtrinsic = UncheckedExtrinsic29            {30                ParachainSystem: cumulus_pallet_parachain_system::{Pallet, Call, Config, Storage, Inherent, Event<T>, ValidateUnsigned} = 20,31                ParachainInfo: parachain_info::{Pallet, Storage, Config} = 21,3233                Aura: pallet_aura::{Pallet, Config<T>} = 22,34                AuraExt: cumulus_pallet_aura_ext::{Pallet, Config} = 23,3536                Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>} = 30,37                RandomnessCollectiveFlip: pallet_randomness_collective_flip::{Pallet, Storage} = 31,38                Timestamp: pallet_timestamp::{Pallet, Call, Storage, Inherent} = 32,39                TransactionPayment: pallet_transaction_payment::{Pallet, Storage} = 33,40                Treasury: pallet_treasury::{Pallet, Call, Storage, Config, Event<T>} = 34,41                Sudo: pallet_sudo::{Pallet, Call, Storage, Config<T>, Event<T>} = 35,42                System: frame_system::{Pallet, Call, Storage, Config, Event<T>} = 36,43                Vesting: orml_vesting::{Pallet, Storage, Call, Event<T>, Config<T>} = 37,44                // Vesting: pallet_vesting::{Pallet, Call, Config<T>, Storage, Event<T>} = 37,45                // Contracts: pallet_contracts::{Pallet, Call, Storage, Event<T>} = 38,4647                // XCM helpers.48                XcmpQueue: cumulus_pallet_xcmp_queue::{Pallet, Call, Storage, Event<T>} = 50,49                PolkadotXcm: pallet_xcm::{Pallet, Call, Event<T>, Origin} = 51,50                CumulusXcm: cumulus_pallet_xcm::{Pallet, Call, Event<T>, Origin} = 52,51                DmpQueue: cumulus_pallet_dmp_queue::{Pallet, Call, Storage, Event<T>} = 53,5253                // Unique Pallets54                Inflation: pallet_inflation::{Pallet, Call, Storage} = 60,55                Unique: pallet_unique::{Pallet, Call, Storage, Event<T>} = 61,5657                #[runtimes(opal)]58                Scheduler: pallet_unique_scheduler::{Pallet, Call, Storage, Event<T>} = 62,5960                // free = 636162                Charging: pallet_charge_transaction::{Pallet, Call, Storage } = 64,63                // ContractHelpers: pallet_contract_helpers::{Pallet, Call, Storage} = 65,64                Common: pallet_common::{Pallet, Storage, Event<T>} = 66,65                Fungible: pallet_fungible::{Pallet, Storage} = 67,6667                #[runtimes(opal)]68                Refungible: pallet_refungible::{Pallet, Storage} = 68,6970                Nonfungible: pallet_nonfungible::{Pallet, Storage} = 69,71                Structure: pallet_structure::{Pallet, Call, Storage, Event<T>} = 70,7273                #[runtimes(opal)]74                RmrkCore: pallet_proxy_rmrk_core::{Pallet, Call, Storage, Event<T>} = 71,7576                #[runtimes(opal)]77                RmrkEquip: pallet_proxy_rmrk_equip::{Pallet, Call, Storage, Event<T>} = 72,7879                // Frontier80                EVM: pallet_evm::{Pallet, Config, Call, Storage, Event<T>} = 100,81                Ethereum: pallet_ethereum::{Pallet, Config, Call, Storage, Event, Origin} = 101,8283                EvmCoderSubstrate: pallet_evm_coder_substrate::{Pallet, Storage} = 150,84                EvmContractHelpers: pallet_evm_contract_helpers::{Pallet, Storage} = 151,85                EvmTransactionPayment: pallet_evm_transaction_payment::{Pallet} = 152,86                EvmMigration: pallet_evm_migration::{Pallet, Call, Storage} = 153,87            }88        }89    }90}
modifiedruntime/opal/Cargo.tomldiffbeforeafterboth
--- a/runtime/opal/Cargo.toml
+++ b/runtime/opal/Cargo.toml
@@ -87,6 +87,7 @@
     'parachain-info/std',
     'serde',
     'pallet-inflation/std',
+    'pallet-configuration/std',
     'pallet-common/std',
     'pallet-structure/std',
     'pallet-fungible/std',
@@ -414,6 +415,7 @@
 fp-evm-mapping = { default-features = false, git = "https://github.com/uniquenetwork/frontier", branch = "unique-polkadot-v0.9.24" }
 pallet-inflation = { path = '../../pallets/inflation', default-features = false }
 up-data-structs = { path = '../../primitives/data-structs', default-features = false }
+pallet-configuration = { default-features = false, path = "../../pallets/configuration" }
 pallet-common = { default-features = false, path = "../../pallets/common" }
 pallet-structure = { default-features = false, path = "../../pallets/structure" }
 pallet-fungible = { default-features = false, path = "../../pallets/fungible" }
modifiedruntime/quartz/Cargo.tomldiffbeforeafterboth
--- a/runtime/quartz/Cargo.toml
+++ b/runtime/quartz/Cargo.toml
@@ -87,6 +87,7 @@
     'parachain-info/std',
     'serde',
     'pallet-inflation/std',
+    'pallet-configuration/std',
     'pallet-common/std',
     'pallet-structure/std',
     'pallet-fungible/std',
@@ -418,6 +419,7 @@
 fp-evm-mapping = { default-features = false, git = "https://github.com/uniquenetwork/frontier", branch = "unique-polkadot-v0.9.24" }
 pallet-inflation = { path = '../../pallets/inflation', default-features = false }
 up-data-structs = { path = '../../primitives/data-structs', default-features = false }
+pallet-configuration = { default-features = false, path = "../../pallets/configuration" }
 pallet-common = { default-features = false, path = "../../pallets/common" }
 pallet-structure = { default-features = false, path = "../../pallets/structure" }
 pallet-fungible = { default-features = false, path = "../../pallets/fungible" }
modifiedruntime/unique/Cargo.tomldiffbeforeafterboth
--- a/runtime/unique/Cargo.toml
+++ b/runtime/unique/Cargo.toml
@@ -88,6 +88,7 @@
     'parachain-info/std',
     'serde',
     'pallet-inflation/std',
+    'pallet-configuration/std',
     'pallet-common/std',
     'pallet-structure/std',
     'pallet-fungible/std',
@@ -411,6 +412,7 @@
 rmrk-rpc = { path = "../../primitives/rmrk-rpc", default-features = false }
 pallet-inflation = { path = '../../pallets/inflation', default-features = false }
 up-data-structs = { path = '../../primitives/data-structs', default-features = false }
+pallet-configuration = { default-features = false, path = "../../pallets/configuration" }
 pallet-common = { default-features = false, path = "../../pallets/common" }
 pallet-structure = { default-features = false, path = "../../pallets/structure" }
 pallet-fungible = { default-features = false, path = "../../pallets/fungible" }