Skip to content
This repository was archived by the owner on Mar 13, 2023. It is now read-only.

Commit 0505dd0

Browse files
Migrate dvm-dynamic-fee to Attribute Macro (#644)
* eliminate warnings * remove `Event` * migrate to attribute macro * Fix test * remove unused `GenesisBuild` * fix compile Co-authored-by: ouwenkg <2630582710@qq.com>
1 parent 86c2667 commit 0505dd0

File tree

9 files changed

+108
-116
lines changed

9 files changed

+108
-116
lines changed

Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/node/cli/src/chain_spec.rs

-2
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,6 @@ fn pangolin_build_spec_genesis() -> pangolin_runtime::GenesisConfig {
393393
},
394394
darwinia_evm: pangolin_runtime::EVMConfig { accounts: evm_accounts },
395395
dvm_ethereum: Default::default(),
396-
dvm_dynamic_fee: Default::default(),
397396
}
398397
}
399398

@@ -551,6 +550,5 @@ fn pangolin_development_genesis() -> pangolin_runtime::GenesisConfig {
551550
},
552551
darwinia_evm: pangolin_runtime::EVMConfig { accounts: evm_accounts },
553552
dvm_ethereum: Default::default(),
554-
dvm_dynamic_fee: Default::default(),
555553
}
556554
}

bin/node/runtime/pangolin/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ frame_support::construct_runtime! {
372372

373373
EVM: darwinia_evm::{Pallet, Call, Storage, Config, Event<T>} = 40,
374374
Ethereum: dvm_ethereum::{Pallet, Call, Storage, Config, Event, ValidateUnsigned} = 41,
375-
DynamicFee: dvm_dynamic_fee::{Pallet, Call, Storage, Config, Event, Inherent} = 47,
375+
DynamicFee: dvm_dynamic_fee::{Pallet, Call, Storage, Inherent} = 47,
376376

377377
BridgeMillauMessages: pallet_bridge_messages::<Instance1>::{Pallet, Call, Storage, Event<T>} = 43,
378378
BridgeMillauDispatch: pallet_bridge_dispatch::<Instance1>::{Pallet, Event<T>} = 44,

bin/node/runtime/pangolin/src/pallets/dynamic_fee.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ use crate::*;
55
use dvm_dynamic_fee::Config;
66

77
frame_support::parameter_types! {
8-
pub BoundDivision: U256 = U256::from(1024);
8+
pub BoundDivision: U256 = 1024.into();
99
}
1010

1111
impl Config for Runtime {
12-
type Event = Event;
1312
type MinGasPriceBoundDivisor = BoundDivision;
1413
}

frame/bridge/crab/backing/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ pub mod pallet {
4545
PalletId,
4646
};
4747
use frame_system::pallet_prelude::*;
48+
#[cfg(feature = "std")]
4849
use sp_runtime::traits::AccountIdConversion;
4950
// --- darwinia ---
5051
use crate::weights::WeightInfo;

frame/bridge/ethereum/backing/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,6 @@ pub mod pallet {
10161016
Ok((term, authorities, beneficiary))
10171017
}
10181018
}
1019-
10201019
impl<T: Config> Sign<BlockNumber<T>> for Pallet<T> {
10211020
type Signature = EcdsaSignature;
10221021
type Message = EcdsaMessage;

frame/dvm-dynamic-fee/Cargo.toml

-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ frame-system = { default-features = false, git = "https://github.com/darwinia-n
2222
sp-core = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.1" }
2323
sp-inherents = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.1" }
2424
sp-runtime = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.1" }
25-
sp-std = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.1" }
2625

2726
[features]
2827
default = ["std"]
@@ -47,5 +46,4 @@ substrate-std = [
4746
"sp-core/std",
4847
"sp-inherents/std",
4948
"sp-runtime/std",
50-
"sp-std/std",
5149
]

frame/dvm-dynamic-fee/src/lib.rs

+102-106
Original file line numberDiff line numberDiff line change
@@ -15,143 +15,139 @@
1515
// See the License for the specific language governing permissions and
1616
// limitations under the License.
1717

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

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>;
4941
}
50-
}
5142

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()
5549
}
56-
);
5750

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();
6160

62-
fn on_initialize(_block_number: T::BlockNumber) -> Weight {
63-
TargetMinGasPrice::kill();
6461
T::DbWeight::get().writes(1)
6562
}
6663

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);
7070

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)));
7572
}
7673
}
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))]
7978
fn note_min_gas_price_target(
80-
origin,
79+
origin: OriginFor<T>,
8180
target: U256,
82-
) {
81+
) -> DispatchResultWithPostInfo {
8382
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-
}
9483

95-
#[derive(Encode, Decode, RuntimeDebug)]
96-
pub enum InherentError {}
84+
<TargetMinGasPrice<T>>::set(Some(target));
9785

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+
}
10193
}
102-
}
10394

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

106-
pub type InherentType = U256;
100+
const INHERENT_IDENTIFIER: InherentIdentifier = INHERENT_IDENTIFIER;
107101

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+
}
110106

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+
}
117110

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+
}
123114
}
124115

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 {}
132121
}
133-
134-
let error = InherentError::decode(&mut &error[..]).ok()?;
135-
Some(Err(Error::Application(Box::from(format!("{:?}", error)))))
136122
}
137-
}
138123

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+
}
148131
}
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+
}
149138

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+
}
153147

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+
}
156151
}
157152
}
153+
pub use pallet::*;

frame/dvm/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,12 @@ use ethereum_types::{Bloom, BloomInput, H160, H256, H64, U256};
4141
use evm::ExitReason;
4242
use sha3::{Digest, Keccak256};
4343
// --- substrate ---
44+
#[cfg(feature = "std")]
45+
use frame_support::storage::unhashed;
4446
use frame_support::{
4547
decl_error, decl_event, decl_module, decl_storage,
4648
dispatch::DispatchResultWithPostInfo,
4749
ensure,
48-
storage::unhashed,
4950
traits::FindAuthor,
5051
traits::{Currency, Get},
5152
weights::Weight,
@@ -65,6 +66,7 @@ use darwinia_evm::{AccountBasic, FeeCalculator, GasWeightMapping, Runner};
6566
use darwinia_support::evm::INTERNAL_CALLER;
6667
use dp_consensus::{PostLog, PreLog, FRONTIER_ENGINE_ID};
6768
use dp_evm::CallOrCreateInfo;
69+
#[cfg(feature = "std")]
6870
use dp_storage::PALLET_ETHEREUM_SCHEMA;
6971

7072
/// A type alias for the balance type from this pallet's point of view.

0 commit comments

Comments
 (0)