diff --git a/frame/dynamic-fee/CHANGELOG.md b/frame/dynamic-fee/CHANGELOG.md index 57c70b394b..87a9144235 100644 --- a/frame/dynamic-fee/CHANGELOG.md +++ b/frame/dynamic-fee/CHANGELOG.md @@ -1,3 +1,5 @@ # Changelog for `pallet-dynamic-fee` -## Unreleased \ No newline at end of file +## Unreleased + +* Implemented evm FeeCalculator for dynamic-fee pallet. \ No newline at end of file diff --git a/frame/dynamic-fee/Cargo.toml b/frame/dynamic-fee/Cargo.toml index 9f9f02506e..dcbf12ca4d 100644 --- a/frame/dynamic-fee/Cargo.toml +++ b/frame/dynamic-fee/Cargo.toml @@ -1,12 +1,13 @@ [package] name = "pallet-dynamic-fee" -version = "1.0.0" +version = "1.0.1-dev" authors = ["Parity Technologies "] edition = "2018" description = "Dynamic fee handling for EVM." license = "Apache-2.0" [dependencies] +pallet-evm = { path = "../evm", version = "3.0.1-dev", default-features = false } codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false } serde = { version = "1.0.101", optional = true } sp-std = { version = "3.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "frontier" } @@ -27,4 +28,5 @@ std = [ "sp-inherents/std", "frame-system/std", "frame-support/std", + "pallet-evm/std", ] diff --git a/frame/dynamic-fee/src/lib.rs b/frame/dynamic-fee/src/lib.rs index 695eb50af9..69ef930ff5 100644 --- a/frame/dynamic-fee/src/lib.rs +++ b/frame/dynamic-fee/src/lib.rs @@ -43,6 +43,11 @@ decl_storage! { MinGasPrice get(fn min_gas_price) config(): U256; TargetMinGasPrice: Option; } + add_extra_genesis { + build(|_config: &GenesisConfig| { + MinGasPrice::set(U256::from(1)); + }); + } } decl_event!( @@ -81,6 +86,12 @@ decl_module! { } } +impl pallet_evm::FeeCalculator for Module { + fn min_gas_price() -> U256 { + MinGasPrice::get() + } +} + #[derive(Encode, Decode, RuntimeDebug)] pub enum InherentError { } @@ -90,6 +101,18 @@ impl IsFatalError for InherentError { } } +impl InherentError { + /// Try to create an instance ouf of the given identifier and data. + #[cfg(feature = "std")] + pub fn try_from(id: &InherentIdentifier, data: &[u8]) -> Option { + if id == &INHERENT_IDENTIFIER { + ::decode(&mut &data[..]).ok() + } else { + None + } + } +} + pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"dynfee0_"; pub type InherentType = U256; @@ -110,8 +133,8 @@ impl ProvideInherentData for InherentDataProvider { inherent_data.put_data(INHERENT_IDENTIFIER, &self.0) } - fn error_to_string(&self, _: &[u8]) -> Option { - None + fn error_to_string(&self, error: &[u8]) -> Option { + InherentError::try_from(&INHERENT_IDENTIFIER, error).map(|e| format!("{:?}", e)) } } diff --git a/template/node/Cargo.toml b/template/node/Cargo.toml index 3db72a28c8..ffa56a11c4 100644 --- a/template/node/Cargo.toml +++ b/template/node/Cargo.toml @@ -42,6 +42,7 @@ sc-consensus = { git = "https://github.com/paritytech/substrate.git", branch = " sp-timestamp = { git = "https://github.com/paritytech/substrate.git", branch = "frontier" } pallet-evm = { path = "../../frame/evm" } pallet-ethereum = { path = "../../frame/ethereum" } +pallet-dynamic-fee = { path = "../../frame/dynamic-fee" } sc-finality-grandpa = { git = "https://github.com/paritytech/substrate.git", branch = "frontier" } sp-finality-grandpa = { git = "https://github.com/paritytech/substrate.git", branch = "frontier" } sc-client-api = { git = "https://github.com/paritytech/substrate.git", branch = "frontier" } diff --git a/template/node/src/chain_spec.rs b/template/node/src/chain_spec.rs index cdf1758cc8..34d1d93ea4 100644 --- a/template/node/src/chain_spec.rs +++ b/template/node/src/chain_spec.rs @@ -172,5 +172,6 @@ fn testnet_genesis( }, }, pallet_ethereum: EthereumConfig {}, + pallet_dynamic_fee: Default::default(), } } diff --git a/template/node/src/cli.rs b/template/node/src/cli.rs index 8704178b47..0410de90d5 100644 --- a/template/node/src/cli.rs +++ b/template/node/src/cli.rs @@ -39,6 +39,10 @@ pub struct RunCmd { /// Maximum number of logs in a query. #[structopt(long, default_value = "10000")] pub max_past_logs: u32, + + /// The dynamic-fee pallet target gas price set by block author + #[structopt(long, default_value = "1")] + pub target_gas_price: u64, } #[derive(Debug, StructOpt)] diff --git a/template/node/src/service.rs b/template/node/src/service.rs index 64219ad97c..652a57b634 100644 --- a/template/node/src/service.rs +++ b/template/node/src/service.rs @@ -20,6 +20,7 @@ use sp_timestamp::InherentError; use sc_telemetry::{Telemetry, TelemetryWorker}; use sc_cli::SubstrateCli; use futures::StreamExt; +use sp_core::U256; use crate::cli::Cli; #[cfg(feature = "manual-seal")] @@ -155,6 +156,10 @@ pub fn new_partial(config: &Configuration, #[allow(unused_variables)] cli: &Cli) .register_provider(MockTimestampInherentDataProvider) .map_err(Into::into) .map_err(sp_consensus::error::Error::InherentData)?; + inherent_data_providers + .register_provider(pallet_dynamic_fee::InherentDataProvider(U256::from(cli.run.target_gas_price))) + .map_err(Into::into) + .map_err(sp_consensus::Error::InherentData)?; let frontier_block_import = FrontierBlockImport::new( client.clone(), @@ -182,6 +187,11 @@ pub fn new_partial(config: &Configuration, #[allow(unused_variables)] cli: &Cli) } #[cfg(feature = "aura")] { + inherent_data_providers + .register_provider(pallet_dynamic_fee::InherentDataProvider(U256::from(cli.run.target_gas_price))) + .map_err(Into::into) + .map_err(sp_consensus::Error::InherentData)?; + let (grandpa_block_import, grandpa_link) = sc_finality_grandpa::block_import( client.clone(), &(client.clone() as Arc<_>), diff --git a/template/runtime/Cargo.toml b/template/runtime/Cargo.toml index 080ecf54b1..0412c6ae02 100644 --- a/template/runtime/Cargo.toml +++ b/template/runtime/Cargo.toml @@ -22,6 +22,7 @@ frame-system-rpc-runtime-api = { version = "3.0.0-dev", default-features = false pallet-ethereum = { default-features = false, path = "../../frame/ethereum" } pallet-evm = { default-features = false, path = "../../frame/evm" } +pallet-dynamic-fee = { default-features = false, path = "../../frame/dynamic-fee" } pallet-evm-precompile-simple = { default-features = false, path = "../../frame/evm/precompile/simple" } pallet-evm-precompile-sha3fips = { default-features = false, path = "../../frame/evm/precompile/sha3fips" } pallet-evm-precompile-modexp = { default-features = false, path = "../../frame/evm/precompile/modexp" } @@ -68,6 +69,7 @@ std = [ "pallet-ethereum/std", "pallet-evm/std", + "pallet-dynamic-fee/std", "pallet-evm-precompile-simple/std", "pallet-evm-precompile-sha3fips/std", "pallet-aura/std", diff --git a/template/runtime/src/lib.rs b/template/runtime/src/lib.rs index 1f840a1eb4..64951739e1 100644 --- a/template/runtime/src/lib.rs +++ b/template/runtime/src/lib.rs @@ -264,15 +264,6 @@ impl pallet_sudo::Config for Runtime { type Call = Call; } -/// Fixed gas price of `1`. -pub struct FixedGasPrice; - -impl FeeCalculator for FixedGasPrice { - fn min_gas_price() -> U256 { - // Gas price is always one token per gas. - 1.into() - } -} parameter_types! { pub const ChainId: u64 = 42; @@ -280,7 +271,7 @@ parameter_types! { } impl pallet_evm::Config for Runtime { - type FeeCalculator = FixedGasPrice; + type FeeCalculator = pallet_dynamic_fee::Module; type GasWeightMapping = (); type CallOrigin = EnsureAddressTruncated; type WithdrawOrigin = EnsureAddressTruncated; @@ -323,6 +314,15 @@ impl pallet_ethereum::Config for Runtime { type StateRoot = pallet_ethereum::IntermediateStateRoot; } +frame_support::parameter_types! { + pub BoundDivision: U256 = U256::from(1024); +} + +impl pallet_dynamic_fee::Config for Runtime { + type Event = Event; + type MinGasPriceBoundDivisor = BoundDivision; +} + // Create the runtime by composing the FRAME pallets that were previously configured. construct_runtime!( pub enum Runtime where @@ -340,6 +340,7 @@ construct_runtime!( Sudo: pallet_sudo::{Module, Call, Config, Storage, Event}, Ethereum: pallet_ethereum::{Module, Call, Storage, Event, Config, ValidateUnsigned}, EVM: pallet_evm::{Module, Config, Call, Storage, Event}, + DynamicFee: pallet_dynamic_fee::{Module, Call, Storage, Config, Event, Inherent}, } );