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

Migrate dvm-dynamic-fee to Attribute Macro #644

Merged
merged 6 commits into from
May 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions bin/node/cli/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,6 @@ fn pangolin_build_spec_genesis() -> pangolin_runtime::GenesisConfig {
},
darwinia_evm: pangolin_runtime::EVMConfig { accounts: evm_accounts },
dvm_ethereum: Default::default(),
dvm_dynamic_fee: Default::default(),
}
}

Expand Down Expand Up @@ -551,6 +550,5 @@ fn pangolin_development_genesis() -> pangolin_runtime::GenesisConfig {
},
darwinia_evm: pangolin_runtime::EVMConfig { accounts: evm_accounts },
dvm_ethereum: Default::default(),
dvm_dynamic_fee: Default::default(),
}
}
2 changes: 1 addition & 1 deletion bin/node/runtime/pangolin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ frame_support::construct_runtime! {

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

BridgeMillauMessages: pallet_bridge_messages::<Instance1>::{Pallet, Call, Storage, Event<T>} = 43,
BridgeMillauDispatch: pallet_bridge_dispatch::<Instance1>::{Pallet, Event<T>} = 44,
Expand Down
3 changes: 1 addition & 2 deletions bin/node/runtime/pangolin/src/pallets/dynamic_fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ use crate::*;
use dvm_dynamic_fee::Config;

frame_support::parameter_types! {
pub BoundDivision: U256 = U256::from(1024);
pub BoundDivision: U256 = 1024.into();
}

impl Config for Runtime {
type Event = Event;
type MinGasPriceBoundDivisor = BoundDivision;
}
1 change: 1 addition & 0 deletions frame/bridge/crab/backing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub mod pallet {
PalletId,
};
use frame_system::pallet_prelude::*;
#[cfg(feature = "std")]
use sp_runtime::traits::AccountIdConversion;
// --- darwinia ---
use crate::weights::WeightInfo;
Expand Down
1 change: 0 additions & 1 deletion frame/bridge/ethereum/backing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,6 @@ pub mod pallet {
Ok((term, authorities, beneficiary))
}
}

impl<T: Config> Sign<BlockNumber<T>> for Pallet<T> {
type Signature = EcdsaSignature;
type Message = EcdsaMessage;
Expand Down
2 changes: 0 additions & 2 deletions frame/dvm-dynamic-fee/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ frame-system = { default-features = false, git = "https://github.com/darwinia-n
sp-core = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.1" }
sp-inherents = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.1" }
sp-runtime = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.1" }
sp-std = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.1" }

[features]
default = ["std"]
Expand All @@ -47,5 +46,4 @@ substrate-std = [
"sp-core/std",
"sp-inherents/std",
"sp-runtime/std",
"sp-std/std",
]
208 changes: 102 additions & 106 deletions frame/dvm-dynamic-fee/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,143 +15,139 @@
// See the License for the specific language governing permissions and
// limitations under the License.

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

use codec::{Decode, Encode};
use frame_support::{
decl_event, decl_module, decl_storage,
inherent::{InherentData, InherentIdentifier, IsFatalError, ProvideInherent},
traits::Get,
weights::Weight,
};
use frame_system::ensure_none;
use sp_core::U256;
#[cfg(feature = "std")]
use sp_inherents::Error;
use sp_runtime::RuntimeDebug;
use sp_std::{
cmp::{max, min},
result,
};

pub trait Config: frame_system::Config {
/// The overarching event type.
type Event: From<Event> + Into<<Self as frame_system::Config>::Event>;
/// Bound divisor for min gas price.
type MinGasPriceBoundDivisor: Get<U256>;
}

decl_storage! {
trait Store for Module<T: Config> as DynamicFee {
MinGasPrice get(fn min_gas_price) config(): U256 = U256::from(1_000_000_000u128);
TargetMinGasPrice: Option<U256>;
#[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>;
}
}

decl_event!(
pub enum Event {
TargetMinGasPriceSet(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()
}
);

decl_module! {
pub struct Module<T: Config> for enum Call where origin: T::Origin {
fn deposit_event() = default;
#[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();

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

fn on_finalize(_block_number: T::BlockNumber) {
if let Some(target) = TargetMinGasPrice::get() {
let bound = MinGasPrice::get() / T::MinGasPriceBoundDivisor::get() + U256::one();
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);

let upper_limit = MinGasPrice::get().saturating_add(bound);
let lower_limit = MinGasPrice::get().saturating_sub(bound);

MinGasPrice::set(min(upper_limit, max(lower_limit, target)));
<MinGasPrice<T>>::set(cmp::min(upper_limit, cmp::max(lower_limit, target)));
}
}

#[weight = T::DbWeight::get().writes(1)]
}
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::weight(T::DbWeight::get().writes(1))]
fn note_min_gas_price_target(
origin,
origin: OriginFor<T>,
target: U256,
) {
) -> DispatchResultWithPostInfo {
ensure_none(origin)?;
TargetMinGasPrice::set(Some(target));
}
}
}

impl<T: Config> darwinia_evm::FeeCalculator for Module<T> {
fn min_gas_price() -> U256 {
MinGasPrice::get()
}
}

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

impl IsFatalError for InherentError {
fn is_fatal_error(&self) -> bool {
match *self {}
Ok(().into())
}
}
impl<T: Config> FeeCalculator for Pallet<T> {
fn min_gas_price() -> U256 {
<MinGasPrice<T>>::get()
}
}
}

pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"dynfee0_";
#[pallet::inherent]
impl<T: Config> ProvideInherent for Pallet<T> {
type Call = Call<T>;
type Error = InherentError;

pub type InherentType = U256;
const INHERENT_IDENTIFIER: InherentIdentifier = INHERENT_IDENTIFIER;

#[cfg(feature = "std")]
pub struct InherentDataProvider(pub InherentType);
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))
}

#[cfg(feature = "std")]
impl InherentDataProvider {
pub fn from_target_gas_price(price: InherentType) -> Self {
Self(price)
}
}
fn check_inherent(_call: &Self::Call, _data: &InherentData) -> Result<(), Self::Error> {
Ok(())
}

#[cfg(feature = "std")]
#[async_trait::async_trait]
impl sp_inherents::InherentDataProvider for InherentDataProvider {
fn provide_inherent_data(&self, inherent_data: &mut InherentData) -> Result<(), Error> {
inherent_data.put_data(INHERENT_IDENTIFIER, &self.0)
fn is_inherent(_: &Self::Call) -> bool {
true
}
}

async fn try_handle_error(
&self,
identifier: &InherentIdentifier,
error: &[u8],
) -> Option<Result<(), Error>> {
if *identifier != INHERENT_IDENTIFIER {
return None;
#[derive(Encode, Decode, RuntimeDebug)]
pub enum InherentError {}
impl IsFatalError for InherentError {
fn is_fatal_error(&self) -> bool {
match *self {}
}

let error = InherentError::decode(&mut &error[..]).ok()?;
Some(Err(Error::Application(Box::from(format!("{:?}", error)))))
}
}

impl<T: Config> ProvideInherent for Module<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))
#[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)
}

fn check_inherent(_call: &Self::Call, _data: &InherentData) -> result::Result<(), Self::Error> {
Ok(())
}
async fn try_handle_error(
&self,
identifier: &InherentIdentifier,
error: &[u8],
) -> Option<Result<(), Error>> {
if *identifier != INHERENT_IDENTIFIER {
return None;
}

fn is_inherent(_: &Self::Call) -> bool {
true
let error = InherentError::decode(&mut &error[..]).ok()?;
Some(Err(Error::Application(Box::from(format!("{:?}", error)))))
}
}
}
pub use pallet::*;
4 changes: 3 additions & 1 deletion frame/dvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ use ethereum_types::{Bloom, BloomInput, H160, H256, H64, U256};
use evm::ExitReason;
use sha3::{Digest, Keccak256};
// --- substrate ---
#[cfg(feature = "std")]
use frame_support::storage::unhashed;
use frame_support::{
decl_error, decl_event, decl_module, decl_storage,
dispatch::DispatchResultWithPostInfo,
ensure,
storage::unhashed,
traits::FindAuthor,
traits::{Currency, Get},
weights::Weight,
Expand All @@ -65,6 +66,7 @@ use darwinia_evm::{AccountBasic, FeeCalculator, GasWeightMapping, Runner};
use darwinia_support::evm::INTERNAL_CALLER;
use dp_consensus::{PostLog, PreLog, FRONTIER_ENGINE_ID};
use dp_evm::CallOrCreateInfo;
#[cfg(feature = "std")]
use dp_storage::PALLET_ETHEREUM_SCHEMA;

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