Skip to content

Commit

Permalink
Dynamic fee pallet for evm's FeeCalculator (#278)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorpaas authored Jan 29, 2021
1 parent bdbabac commit 761083c
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[workspace]
members = [
"frame/dynamic-fee",
"frame/ethereum",
"frame/evm",
"frame/evm/precompile/simple",
Expand Down
30 changes: 30 additions & 0 deletions frame/dynamic-fee/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[package]
name = "pallet-dynamic-fee"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
description = "Dynamic fee handling for EVM."
license = "Apache-2.0"

[dependencies]
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false }
serde = { version = "1.0.101", optional = true }
sp-std = { version = "2.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "frontier" }
sp-core = { version = "2.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "frontier" }
sp-runtime = { version = "2.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "frontier" }
sp-inherents = { version = "2.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "frontier" }
frame-system = { version = "2.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "frontier" }
frame-support = { version = "2.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "frontier" }

[features]
default = ["std"]
std = [
"codec/std",
"serde",
"sp-std/std",
"sp-core/std",
"sp-runtime/std",
"sp-inherents/std",
"frame-system/std",
"frame-support/std",
]
132 changes: 132 additions & 0 deletions frame/dynamic-fee/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// SPDX-License-Identifier: Apache-2.0
// This file is part of Frontier.
//
// Copyright (c) 2021 Parity Technologies (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Ensure we're `no_std` when compiling for Wasm.
#![cfg_attr(not(feature = "std"), no_std)]

use codec::{Encode, Decode};
use sp_std::{result, cmp::{min, max}};
use sp_runtime::RuntimeDebug;
use sp_core::U256;
use sp_inherents::{InherentIdentifier, InherentData, ProvideInherent, IsFatalError};
#[cfg(feature = "std")]
use sp_inherents::ProvideInherentData;
use frame_support::{
decl_module, decl_storage, decl_event,
traits::Get,
};
use frame_system::ensure_none;

pub trait Config: frame_system::Config {
/// The overarching event type.
type Event: From<Event> + Into<<Self as frame_system::Config>::Event>;
/// Bound divisor for min gas price.
type MinGasPriceBoundDivisor: Get<U256>;
}

decl_storage! {
trait Store for Module<T: Config> as DynamicFee {
MinGasPrice get(fn min_gas_price) config(): U256;
TargetMinGasPrice: Option<U256>;
}
}

decl_event!(
pub enum Event {
TargetMinGasPriceSet(U256),
}
);

decl_module! {
pub struct Module<T: Config> for enum Call where origin: T::Origin {
fn deposit_event() = default;

fn on_finalize(n: T::BlockNumber) {
if let Some(target) = TargetMinGasPrice::get() {
let bound = MinGasPrice::get() / T::MinGasPriceBoundDivisor::get() + U256::one();

let upper_limit = MinGasPrice::get().saturating_add(bound);
let lower_limit = MinGasPrice::get().saturating_sub(bound);

MinGasPrice::set(min(upper_limit, max(lower_limit, target)));
}

TargetMinGasPrice::kill();
}

#[weight = 0]
fn note_min_gas_price_target(
origin,
target: U256,
) {
ensure_none(origin)?;

TargetMinGasPrice::set(Some(target));
Self::deposit_event(Event::TargetMinGasPriceSet(target));
}
}
}

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

impl IsFatalError for InherentError {
fn is_fatal_error(&self) -> bool {
match *self { }
}
}

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

pub type InherentType = U256;

#[cfg(feature = "std")]
pub struct InherentDataProvider(pub InherentType);

#[cfg(feature = "std")]
impl ProvideInherentData for InherentDataProvider {
fn inherent_identifier(&self) -> &'static InherentIdentifier {
&INHERENT_IDENTIFIER
}

fn provide_inherent_data(
&self,
inherent_data: &mut InherentData
) -> Result<(), sp_inherents::Error> {
inherent_data.put_data(INHERENT_IDENTIFIER, &self.0)
}

fn error_to_string(&self, _: &[u8]) -> Option<String> {
None
}
}

impl<T: Config> ProvideInherent for Module<T> {
type Call = Call<T>;
type Error = InherentError;
const INHERENT_IDENTIFIER: InherentIdentifier = INHERENT_IDENTIFIER;

fn create_inherent(data: &InherentData) -> Option<Self::Call> {
let target = data.get_data::<InherentType>(&INHERENT_IDENTIFIER).ok()??;

Some(Call::note_min_gas_price_target(target))
}

fn check_inherent(_call: &Self::Call, _data: &InherentData) -> result::Result<(), Self::Error> {
Ok(())
}
}

0 comments on commit 761083c

Please sign in to comment.