This repository was archived by the owner on Mar 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlib.rs
153 lines (131 loc) · 4.19 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// 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.
#![cfg_attr(not(feature = "std"), no_std)]
#[frame_support::pallet]
pub mod pallet {
// --- core ---
use core::cmp;
// --- substrate ---
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use sp_core::U256;
#[cfg(feature = "std")]
use sp_inherents::InherentDataProvider as InherentDataProviderT;
use sp_inherents::{Error, IsFatalError};
// --- darwinia ---
use darwinia_evm::FeeCalculator;
pub type InherentType = U256;
pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"dynfee0_";
#[pallet::config]
pub trait Config: frame_system::Config {
type MinGasPriceBoundDivisor: Get<U256>;
}
#[pallet::storage]
#[pallet::getter(fn min_gas_price)]
pub type MinGasPrice<T> = StorageValue<_, U256, ValueQuery, DefaultForMinGasPrice>;
#[pallet::type_value]
pub fn DefaultForMinGasPrice() -> U256 {
1_000_000_000_u128.into()
}
#[pallet::storage]
pub type TargetMinGasPrice<T> = StorageValue<_, U256, OptionQuery>;
#[pallet::pallet]
pub struct Pallet<T>(_);
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(_: BlockNumberFor<T>) -> Weight {
<TargetMinGasPrice<T>>::kill();
T::DbWeight::get().writes(1)
}
fn on_finalize(_: BlockNumberFor<T>) {
if let Some(target) = <TargetMinGasPrice<T>>::get() {
let bound =
<MinGasPrice<T>>::get() / T::MinGasPriceBoundDivisor::get() + U256::one();
let upper_limit = <MinGasPrice<T>>::get().saturating_add(bound);
let lower_limit = <MinGasPrice<T>>::get().saturating_sub(bound);
<MinGasPrice<T>>::set(cmp::min(upper_limit, cmp::max(lower_limit, target)));
}
}
}
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::weight(T::DbWeight::get().writes(1))]
fn note_min_gas_price_target(
origin: OriginFor<T>,
target: U256,
) -> DispatchResultWithPostInfo {
ensure_none(origin)?;
<TargetMinGasPrice<T>>::set(Some(target));
Ok(().into())
}
}
impl<T: Config> FeeCalculator for Pallet<T> {
fn min_gas_price() -> U256 {
<MinGasPrice<T>>::get()
}
}
#[pallet::inherent]
impl<T: Config> ProvideInherent for Pallet<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<(), Self::Error> {
Ok(())
}
fn is_inherent(_: &Self::Call) -> bool {
true
}
}
#[derive(Encode, Decode, RuntimeDebug)]
pub enum InherentError {}
impl IsFatalError for InherentError {
fn is_fatal_error(&self) -> bool {
match *self {}
}
}
#[cfg(feature = "std")]
pub struct InherentDataProvider(pub InherentType);
#[cfg(feature = "std")]
impl InherentDataProvider {
pub fn from_target_gas_price(price: InherentType) -> Self {
Self(price)
}
}
#[cfg(feature = "std")]
#[async_trait::async_trait]
impl InherentDataProviderT for InherentDataProvider {
fn provide_inherent_data(&self, inherent_data: &mut InherentData) -> Result<(), Error> {
inherent_data.put_data(INHERENT_IDENTIFIER, &self.0)
}
async fn try_handle_error(
&self,
identifier: &InherentIdentifier,
error: &[u8],
) -> Option<Result<(), Error>> {
if *identifier != INHERENT_IDENTIFIER {
return None;
}
let error = InherentError::decode(&mut &error[..]).ok()?;
Some(Err(Error::Application(Box::from(format!("{:?}", error)))))
}
}
}
pub use pallet::*;