Skip to content

Commit 761083c

Browse files
authored
Dynamic fee pallet for evm's FeeCalculator (#278)
1 parent bdbabac commit 761083c

4 files changed

Lines changed: 177 additions & 0 deletions

File tree

Cargo.lock

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[workspace]
22
members = [
3+
"frame/dynamic-fee",
34
"frame/ethereum",
45
"frame/evm",
56
"frame/evm/precompile/simple",

frame/dynamic-fee/Cargo.toml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[package]
2+
name = "pallet-dynamic-fee"
3+
version = "0.1.0"
4+
authors = ["Parity Technologies <[email protected]>"]
5+
edition = "2018"
6+
description = "Dynamic fee handling for EVM."
7+
license = "Apache-2.0"
8+
9+
[dependencies]
10+
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false }
11+
serde = { version = "1.0.101", optional = true }
12+
sp-std = { version = "2.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "frontier" }
13+
sp-core = { version = "2.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "frontier" }
14+
sp-runtime = { version = "2.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "frontier" }
15+
sp-inherents = { version = "2.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "frontier" }
16+
frame-system = { version = "2.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "frontier" }
17+
frame-support = { version = "2.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "frontier" }
18+
19+
[features]
20+
default = ["std"]
21+
std = [
22+
"codec/std",
23+
"serde",
24+
"sp-std/std",
25+
"sp-core/std",
26+
"sp-runtime/std",
27+
"sp-inherents/std",
28+
"frame-system/std",
29+
"frame-support/std",
30+
]

frame/dynamic-fee/src/lib.rs

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// This file is part of Frontier.
3+
//
4+
// Copyright (c) 2021 Parity Technologies (UK) Ltd.
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
18+
// Ensure we're `no_std` when compiling for Wasm.
19+
#![cfg_attr(not(feature = "std"), no_std)]
20+
21+
use codec::{Encode, Decode};
22+
use sp_std::{result, cmp::{min, max}};
23+
use sp_runtime::RuntimeDebug;
24+
use sp_core::U256;
25+
use sp_inherents::{InherentIdentifier, InherentData, ProvideInherent, IsFatalError};
26+
#[cfg(feature = "std")]
27+
use sp_inherents::ProvideInherentData;
28+
use frame_support::{
29+
decl_module, decl_storage, decl_event,
30+
traits::Get,
31+
};
32+
use frame_system::ensure_none;
33+
34+
pub trait Config: frame_system::Config {
35+
/// The overarching event type.
36+
type Event: From<Event> + Into<<Self as frame_system::Config>::Event>;
37+
/// Bound divisor for min gas price.
38+
type MinGasPriceBoundDivisor: Get<U256>;
39+
}
40+
41+
decl_storage! {
42+
trait Store for Module<T: Config> as DynamicFee {
43+
MinGasPrice get(fn min_gas_price) config(): U256;
44+
TargetMinGasPrice: Option<U256>;
45+
}
46+
}
47+
48+
decl_event!(
49+
pub enum Event {
50+
TargetMinGasPriceSet(U256),
51+
}
52+
);
53+
54+
decl_module! {
55+
pub struct Module<T: Config> for enum Call where origin: T::Origin {
56+
fn deposit_event() = default;
57+
58+
fn on_finalize(n: T::BlockNumber) {
59+
if let Some(target) = TargetMinGasPrice::get() {
60+
let bound = MinGasPrice::get() / T::MinGasPriceBoundDivisor::get() + U256::one();
61+
62+
let upper_limit = MinGasPrice::get().saturating_add(bound);
63+
let lower_limit = MinGasPrice::get().saturating_sub(bound);
64+
65+
MinGasPrice::set(min(upper_limit, max(lower_limit, target)));
66+
}
67+
68+
TargetMinGasPrice::kill();
69+
}
70+
71+
#[weight = 0]
72+
fn note_min_gas_price_target(
73+
origin,
74+
target: U256,
75+
) {
76+
ensure_none(origin)?;
77+
78+
TargetMinGasPrice::set(Some(target));
79+
Self::deposit_event(Event::TargetMinGasPriceSet(target));
80+
}
81+
}
82+
}
83+
84+
#[derive(Encode, Decode, RuntimeDebug)]
85+
pub enum InherentError { }
86+
87+
impl IsFatalError for InherentError {
88+
fn is_fatal_error(&self) -> bool {
89+
match *self { }
90+
}
91+
}
92+
93+
pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"dynfee0_";
94+
95+
pub type InherentType = U256;
96+
97+
#[cfg(feature = "std")]
98+
pub struct InherentDataProvider(pub InherentType);
99+
100+
#[cfg(feature = "std")]
101+
impl ProvideInherentData for InherentDataProvider {
102+
fn inherent_identifier(&self) -> &'static InherentIdentifier {
103+
&INHERENT_IDENTIFIER
104+
}
105+
106+
fn provide_inherent_data(
107+
&self,
108+
inherent_data: &mut InherentData
109+
) -> Result<(), sp_inherents::Error> {
110+
inherent_data.put_data(INHERENT_IDENTIFIER, &self.0)
111+
}
112+
113+
fn error_to_string(&self, _: &[u8]) -> Option<String> {
114+
None
115+
}
116+
}
117+
118+
impl<T: Config> ProvideInherent for Module<T> {
119+
type Call = Call<T>;
120+
type Error = InherentError;
121+
const INHERENT_IDENTIFIER: InherentIdentifier = INHERENT_IDENTIFIER;
122+
123+
fn create_inherent(data: &InherentData) -> Option<Self::Call> {
124+
let target = data.get_data::<InherentType>(&INHERENT_IDENTIFIER).ok()??;
125+
126+
Some(Call::note_min_gas_price_target(target))
127+
}
128+
129+
fn check_inherent(_call: &Self::Call, _data: &InherentData) -> result::Result<(), Self::Error> {
130+
Ok(())
131+
}
132+
}

0 commit comments

Comments
 (0)