|
| 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