|
15 | 15 | // See the License for the specific language governing permissions and
|
16 | 16 | // limitations under the License.
|
17 | 17 |
|
18 |
| -// Ensure we're `no_std` when compiling for Wasm. |
19 | 18 | #![cfg_attr(not(feature = "std"), no_std)]
|
20 | 19 |
|
21 |
| -use codec::{Decode, Encode}; |
22 |
| -use frame_support::{ |
23 |
| - decl_event, decl_module, decl_storage, |
24 |
| - inherent::{InherentData, InherentIdentifier, IsFatalError, ProvideInherent}, |
25 |
| - traits::Get, |
26 |
| - weights::Weight, |
27 |
| -}; |
28 |
| -use frame_system::ensure_none; |
29 |
| -use sp_core::U256; |
30 |
| -#[cfg(feature = "std")] |
31 |
| -use sp_inherents::Error; |
32 |
| -use sp_runtime::RuntimeDebug; |
33 |
| -use sp_std::{ |
34 |
| - cmp::{max, min}, |
35 |
| - result, |
36 |
| -}; |
37 |
| - |
38 |
| -pub trait Config: frame_system::Config { |
39 |
| - /// The overarching event type. |
40 |
| - type Event: From<Event> + Into<<Self as frame_system::Config>::Event>; |
41 |
| - /// Bound divisor for min gas price. |
42 |
| - type MinGasPriceBoundDivisor: Get<U256>; |
43 |
| -} |
44 |
| - |
45 |
| -decl_storage! { |
46 |
| - trait Store for Module<T: Config> as DynamicFee { |
47 |
| - MinGasPrice get(fn min_gas_price) config(): U256 = U256::from(1_000_000_000u128); |
48 |
| - TargetMinGasPrice: Option<U256>; |
| 20 | +#[frame_support::pallet] |
| 21 | +pub mod pallet { |
| 22 | + // --- core --- |
| 23 | + use core::cmp; |
| 24 | + // --- substrate --- |
| 25 | + use frame_support::pallet_prelude::*; |
| 26 | + use frame_system::pallet_prelude::*; |
| 27 | + use sp_core::U256; |
| 28 | + #[cfg(feature = "std")] |
| 29 | + use sp_inherents::InherentDataProvider as InherentDataProviderT; |
| 30 | + use sp_inherents::{Error, IsFatalError}; |
| 31 | + // --- darwinia --- |
| 32 | + use darwinia_evm::FeeCalculator; |
| 33 | + |
| 34 | + pub type InherentType = U256; |
| 35 | + |
| 36 | + pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"dynfee0_"; |
| 37 | + |
| 38 | + #[pallet::config] |
| 39 | + pub trait Config: frame_system::Config { |
| 40 | + type MinGasPriceBoundDivisor: Get<U256>; |
49 | 41 | }
|
50 |
| -} |
51 | 42 |
|
52 |
| -decl_event!( |
53 |
| - pub enum Event { |
54 |
| - TargetMinGasPriceSet(U256), |
| 43 | + #[pallet::storage] |
| 44 | + #[pallet::getter(fn min_gas_price)] |
| 45 | + pub type MinGasPrice<T> = StorageValue<_, U256, ValueQuery, DefaultForMinGasPrice>; |
| 46 | + #[pallet::type_value] |
| 47 | + pub fn DefaultForMinGasPrice() -> U256 { |
| 48 | + 1_000_000_000_u128.into() |
55 | 49 | }
|
56 |
| -); |
57 | 50 |
|
58 |
| -decl_module! { |
59 |
| - pub struct Module<T: Config> for enum Call where origin: T::Origin { |
60 |
| - fn deposit_event() = default; |
| 51 | + #[pallet::storage] |
| 52 | + pub type TargetMinGasPrice<T> = StorageValue<_, U256, OptionQuery>; |
| 53 | + |
| 54 | + #[pallet::pallet] |
| 55 | + pub struct Pallet<T>(_); |
| 56 | + #[pallet::hooks] |
| 57 | + impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> { |
| 58 | + fn on_initialize(_: BlockNumberFor<T>) -> Weight { |
| 59 | + <TargetMinGasPrice<T>>::kill(); |
61 | 60 |
|
62 |
| - fn on_initialize(_block_number: T::BlockNumber) -> Weight { |
63 |
| - TargetMinGasPrice::kill(); |
64 | 61 | T::DbWeight::get().writes(1)
|
65 | 62 | }
|
66 | 63 |
|
67 |
| - fn on_finalize(_block_number: T::BlockNumber) { |
68 |
| - if let Some(target) = TargetMinGasPrice::get() { |
69 |
| - let bound = MinGasPrice::get() / T::MinGasPriceBoundDivisor::get() + U256::one(); |
| 64 | + fn on_finalize(_: BlockNumberFor<T>) { |
| 65 | + if let Some(target) = <TargetMinGasPrice<T>>::get() { |
| 66 | + let bound = |
| 67 | + <MinGasPrice<T>>::get() / T::MinGasPriceBoundDivisor::get() + U256::one(); |
| 68 | + let upper_limit = <MinGasPrice<T>>::get().saturating_add(bound); |
| 69 | + let lower_limit = <MinGasPrice<T>>::get().saturating_sub(bound); |
70 | 70 |
|
71 |
| - let upper_limit = MinGasPrice::get().saturating_add(bound); |
72 |
| - let lower_limit = MinGasPrice::get().saturating_sub(bound); |
73 |
| - |
74 |
| - MinGasPrice::set(min(upper_limit, max(lower_limit, target))); |
| 71 | + <MinGasPrice<T>>::set(cmp::min(upper_limit, cmp::max(lower_limit, target))); |
75 | 72 | }
|
76 | 73 | }
|
77 |
| - |
78 |
| - #[weight = T::DbWeight::get().writes(1)] |
| 74 | + } |
| 75 | + #[pallet::call] |
| 76 | + impl<T: Config> Pallet<T> { |
| 77 | + #[pallet::weight(T::DbWeight::get().writes(1))] |
79 | 78 | fn note_min_gas_price_target(
|
80 |
| - origin, |
| 79 | + origin: OriginFor<T>, |
81 | 80 | target: U256,
|
82 |
| - ) { |
| 81 | + ) -> DispatchResultWithPostInfo { |
83 | 82 | ensure_none(origin)?;
|
84 |
| - TargetMinGasPrice::set(Some(target)); |
85 |
| - } |
86 |
| - } |
87 |
| -} |
88 |
| - |
89 |
| -impl<T: Config> darwinia_evm::FeeCalculator for Module<T> { |
90 |
| - fn min_gas_price() -> U256 { |
91 |
| - MinGasPrice::get() |
92 |
| - } |
93 |
| -} |
94 | 83 |
|
95 |
| -#[derive(Encode, Decode, RuntimeDebug)] |
96 |
| -pub enum InherentError {} |
| 84 | + <TargetMinGasPrice<T>>::set(Some(target)); |
97 | 85 |
|
98 |
| -impl IsFatalError for InherentError { |
99 |
| - fn is_fatal_error(&self) -> bool { |
100 |
| - match *self {} |
| 86 | + Ok(().into()) |
| 87 | + } |
| 88 | + } |
| 89 | + impl<T: Config> FeeCalculator for Pallet<T> { |
| 90 | + fn min_gas_price() -> U256 { |
| 91 | + <MinGasPrice<T>>::get() |
| 92 | + } |
101 | 93 | }
|
102 |
| -} |
103 | 94 |
|
104 |
| -pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"dynfee0_"; |
| 95 | + #[pallet::inherent] |
| 96 | + impl<T: Config> ProvideInherent for Pallet<T> { |
| 97 | + type Call = Call<T>; |
| 98 | + type Error = InherentError; |
105 | 99 |
|
106 |
| -pub type InherentType = U256; |
| 100 | + const INHERENT_IDENTIFIER: InherentIdentifier = INHERENT_IDENTIFIER; |
107 | 101 |
|
108 |
| -#[cfg(feature = "std")] |
109 |
| -pub struct InherentDataProvider(pub InherentType); |
| 102 | + fn create_inherent(data: &InherentData) -> Option<Self::Call> { |
| 103 | + let target = data.get_data::<InherentType>(&INHERENT_IDENTIFIER).ok()??; |
| 104 | + Some(Call::note_min_gas_price_target(target)) |
| 105 | + } |
110 | 106 |
|
111 |
| -#[cfg(feature = "std")] |
112 |
| -impl InherentDataProvider { |
113 |
| - pub fn from_target_gas_price(price: InherentType) -> Self { |
114 |
| - Self(price) |
115 |
| - } |
116 |
| -} |
| 107 | + fn check_inherent(_call: &Self::Call, _data: &InherentData) -> Result<(), Self::Error> { |
| 108 | + Ok(()) |
| 109 | + } |
117 | 110 |
|
118 |
| -#[cfg(feature = "std")] |
119 |
| -#[async_trait::async_trait] |
120 |
| -impl sp_inherents::InherentDataProvider for InherentDataProvider { |
121 |
| - fn provide_inherent_data(&self, inherent_data: &mut InherentData) -> Result<(), Error> { |
122 |
| - inherent_data.put_data(INHERENT_IDENTIFIER, &self.0) |
| 111 | + fn is_inherent(_: &Self::Call) -> bool { |
| 112 | + true |
| 113 | + } |
123 | 114 | }
|
124 | 115 |
|
125 |
| - async fn try_handle_error( |
126 |
| - &self, |
127 |
| - identifier: &InherentIdentifier, |
128 |
| - error: &[u8], |
129 |
| - ) -> Option<Result<(), Error>> { |
130 |
| - if *identifier != INHERENT_IDENTIFIER { |
131 |
| - return None; |
| 116 | + #[derive(Encode, Decode, RuntimeDebug)] |
| 117 | + pub enum InherentError {} |
| 118 | + impl IsFatalError for InherentError { |
| 119 | + fn is_fatal_error(&self) -> bool { |
| 120 | + match *self {} |
132 | 121 | }
|
133 |
| - |
134 |
| - let error = InherentError::decode(&mut &error[..]).ok()?; |
135 |
| - Some(Err(Error::Application(Box::from(format!("{:?}", error))))) |
136 | 122 | }
|
137 |
| -} |
138 | 123 |
|
139 |
| -impl<T: Config> ProvideInherent for Module<T> { |
140 |
| - type Call = Call<T>; |
141 |
| - type Error = InherentError; |
142 |
| - const INHERENT_IDENTIFIER: InherentIdentifier = INHERENT_IDENTIFIER; |
143 |
| - |
144 |
| - fn create_inherent(data: &InherentData) -> Option<Self::Call> { |
145 |
| - let target = data.get_data::<InherentType>(&INHERENT_IDENTIFIER).ok()??; |
146 |
| - |
147 |
| - Some(Call::note_min_gas_price_target(target)) |
| 124 | + #[cfg(feature = "std")] |
| 125 | + pub struct InherentDataProvider(pub InherentType); |
| 126 | + #[cfg(feature = "std")] |
| 127 | + impl InherentDataProvider { |
| 128 | + pub fn from_target_gas_price(price: InherentType) -> Self { |
| 129 | + Self(price) |
| 130 | + } |
148 | 131 | }
|
| 132 | + #[cfg(feature = "std")] |
| 133 | + #[async_trait::async_trait] |
| 134 | + impl InherentDataProviderT for InherentDataProvider { |
| 135 | + fn provide_inherent_data(&self, inherent_data: &mut InherentData) -> Result<(), Error> { |
| 136 | + inherent_data.put_data(INHERENT_IDENTIFIER, &self.0) |
| 137 | + } |
149 | 138 |
|
150 |
| - fn check_inherent(_call: &Self::Call, _data: &InherentData) -> result::Result<(), Self::Error> { |
151 |
| - Ok(()) |
152 |
| - } |
| 139 | + async fn try_handle_error( |
| 140 | + &self, |
| 141 | + identifier: &InherentIdentifier, |
| 142 | + error: &[u8], |
| 143 | + ) -> Option<Result<(), Error>> { |
| 144 | + if *identifier != INHERENT_IDENTIFIER { |
| 145 | + return None; |
| 146 | + } |
153 | 147 |
|
154 |
| - fn is_inherent(_: &Self::Call) -> bool { |
155 |
| - true |
| 148 | + let error = InherentError::decode(&mut &error[..]).ok()?; |
| 149 | + Some(Err(Error::Application(Box::from(format!("{:?}", error))))) |
| 150 | + } |
156 | 151 | }
|
157 | 152 | }
|
| 153 | +pub use pallet::*; |
0 commit comments