Skip to content

Commit

Permalink
Update gas_price with dynamic-fee pallet (#389)
Browse files Browse the repository at this point in the history
* Use dynamic-fee pallet

* Update pallet-evm dep version

* Update dynamic-fee version and add changelog
  • Loading branch information
boundless-forest authored May 14, 2021
1 parent 33e9bfa commit 3c3f377
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 14 deletions.
4 changes: 3 additions & 1 deletion frame/dynamic-fee/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Changelog for `pallet-dynamic-fee`

## Unreleased
## Unreleased

* Implemented evm FeeCalculator for dynamic-fee pallet.
4 changes: 3 additions & 1 deletion frame/dynamic-fee/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
[package]
name = "pallet-dynamic-fee"
version = "1.0.0"
version = "1.0.1-dev"
authors = ["Parity Technologies <admin@parity.io>"]
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" }
Expand All @@ -27,4 +28,5 @@ std = [
"sp-inherents/std",
"frame-system/std",
"frame-support/std",
"pallet-evm/std",
]
27 changes: 25 additions & 2 deletions frame/dynamic-fee/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ decl_storage! {
MinGasPrice get(fn min_gas_price) config(): U256;
TargetMinGasPrice: Option<U256>;
}
add_extra_genesis {
build(|_config: &GenesisConfig| {
MinGasPrice::set(U256::from(1));
});
}
}

decl_event!(
Expand Down Expand Up @@ -81,6 +86,12 @@ decl_module! {
}
}

impl<T: Config> pallet_evm::FeeCalculator for Module<T> {
fn min_gas_price() -> U256 {
MinGasPrice::get()
}
}

#[derive(Encode, Decode, RuntimeDebug)]
pub enum InherentError { }

Expand All @@ -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<Self> {
if id == &INHERENT_IDENTIFIER {
<InherentError as codec::Decode>::decode(&mut &data[..]).ok()
} else {
None
}
}
}

pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"dynfee0_";

pub type InherentType = U256;
Expand All @@ -110,8 +133,8 @@ impl ProvideInherentData for InherentDataProvider {
inherent_data.put_data(INHERENT_IDENTIFIER, &self.0)
}

fn error_to_string(&self, _: &[u8]) -> Option<String> {
None
fn error_to_string(&self, error: &[u8]) -> Option<String> {
InherentError::try_from(&INHERENT_IDENTIFIER, error).map(|e| format!("{:?}", e))
}
}

Expand Down
1 change: 1 addition & 0 deletions template/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
1 change: 1 addition & 0 deletions template/node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,5 +172,6 @@ fn testnet_genesis(
},
},
pallet_ethereum: EthereumConfig {},
pallet_dynamic_fee: Default::default(),
}
}
4 changes: 4 additions & 0 deletions template/node/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
10 changes: 10 additions & 0 deletions template/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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<_>),
Expand Down
2 changes: 2 additions & 0 deletions template/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down Expand Up @@ -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",
Expand Down
21 changes: 11 additions & 10 deletions template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,23 +264,14 @@ 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;
pub BlockGasLimit: U256 = U256::from(u32::max_value());
}

impl pallet_evm::Config for Runtime {
type FeeCalculator = FixedGasPrice;
type FeeCalculator = pallet_dynamic_fee::Module<Self>;
type GasWeightMapping = ();
type CallOrigin = EnsureAddressTruncated;
type WithdrawOrigin = EnsureAddressTruncated;
Expand Down Expand Up @@ -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
Expand All @@ -340,6 +340,7 @@ construct_runtime!(
Sudo: pallet_sudo::{Module, Call, Config<T>, Storage, Event<T>},
Ethereum: pallet_ethereum::{Module, Call, Storage, Event, Config, ValidateUnsigned},
EVM: pallet_evm::{Module, Config, Call, Storage, Event<T>},
DynamicFee: pallet_dynamic_fee::{Module, Call, Storage, Config, Event, Inherent},
}
);

Expand Down

0 comments on commit 3c3f377

Please sign in to comment.