Skip to content

Commit

Permalink
Merge branch 'master' into update-polkadot-v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sorpaas authored Jul 27, 2023
2 parents 2b1b8d0 + b27304a commit f36ce90
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 19 deletions.
4 changes: 3 additions & 1 deletion client/rpc/src/eth/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ where
.await?
{
let Ok(number) = self.client.expect_block_number_from_id(&id) else {
return Err(internal_err(format!("Failed to retrieve block number at {id}")));
return Err(internal_err(format!(
"Failed to retrieve block number at {id}"
)));
};
// Highest and lowest block number within the requested range.
let highest = UniqueSaturatedInto::<u64>::unique_saturated_into(number);
Expand Down
13 changes: 12 additions & 1 deletion frame/base-fee/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,18 @@ pub mod pallet {
let decrease = scaled_basefee
.checked_div(U256::from(1_000_000))
.unwrap_or_else(U256::zero);
*bf = bf.saturating_sub(decrease);
let default_base_fee = T::DefaultBaseFeePerGas::get();
// lowest fee is norm(DefaultBaseFeePerGas * Threshold::ideal()):
let lowest_base_fee = default_base_fee
.checked_mul(U256::from(T::Threshold::ideal().deconstruct()))
.unwrap_or(default_base_fee)
.checked_div(U256::from(1_000_000))
.unwrap_or(default_base_fee);
if bf.saturating_sub(decrease) >= lowest_base_fee {
*bf = bf.saturating_sub(decrease);
} else {
*bf = lowest_base_fee;
}
} else {
Self::deposit_event(Event::BaseFeeOverflow);
}
Expand Down
23 changes: 14 additions & 9 deletions frame/base-fee/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use sp_runtime::{

use super::*;
use crate as pallet_base_fee;
use crate::BaseFeeThreshold as BaseFeeThresholdT;

type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;
Expand Down Expand Up @@ -75,7 +76,7 @@ parameter_types! {
}

pub struct BaseFeeThreshold;
impl pallet_base_fee::BaseFeeThreshold for BaseFeeThreshold {
impl BaseFeeThresholdT for BaseFeeThreshold {
fn lower() -> Permill {
Permill::zero()
}
Expand Down Expand Up @@ -132,6 +133,15 @@ pub fn new_test_ext(base_fee: Option<U256>, elasticity: Option<Permill>) -> Test
TestExternalities::new(t)
}

pub fn get_lowest_base_fee() -> U256 {
let default_base_fee = DefaultBaseFeePerGas::get();
default_base_fee
.checked_mul(U256::from(BaseFeeThreshold::ideal().deconstruct()))
.unwrap_or(default_base_fee)
.checked_div(U256::from(1_000_000))
.unwrap_or(default_base_fee)
}

#[test]
fn should_default() {
new_test_ext(None, None).execute_with(|| {
Expand All @@ -158,12 +168,11 @@ fn should_not_overflow_u256() {
}

#[test]
fn should_handle_zero() {
fn should_fallback_to_default_value() {
let base_fee = U256::zero();
new_test_ext(Some(base_fee), None).execute_with(|| {
let init = BaseFeePerGas::<Test>::get();
BaseFee::on_finalize(System::block_number());
assert_eq!(BaseFeePerGas::<Test>::get(), init);
assert_eq!(BaseFeePerGas::<Test>::get(), get_lowest_base_fee());
});
}

Expand All @@ -175,11 +184,7 @@ fn should_handle_consecutive_empty_blocks() {
BaseFee::on_finalize(System::block_number());
System::set_block_number(System::block_number() + 1);
}
assert_eq!(
BaseFeePerGas::<Test>::get(),
// 8 is the lowest number which's 12.5% is >= 1.
U256::from(7)
);
assert_eq!(BaseFeePerGas::<Test>::get(), get_lowest_base_fee());
});
let zero_elasticity = Permill::zero();
new_test_ext(Some(base_fee), Some(zero_elasticity)).execute_with(|| {
Expand Down
9 changes: 7 additions & 2 deletions frame/evm/src/runner/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,15 +261,20 @@ where
_ => used_gas.into(),
};
let actual_fee = effective_gas.saturating_mul(total_fee_per_gas);
let actual_base_fee = effective_gas.saturating_mul(base_fee);

log::debug!(
target: "evm",
"Execution {:?} [source: {:?}, value: {}, gas_limit: {}, actual_fee: {}, is_transactional: {}]",
"Execution {:?} [source: {:?}, value: {}, gas_limit: {}, actual_fee: {}, used_gas: {}, effective_gas: {}, base_fee: {}, total_fee_per_gas: {}, is_transactional: {}]",
reason,
source,
value,
gas_limit,
actual_fee,
used_gas,
effective_gas,
base_fee,
total_fee_per_gas,
is_transactional
);
// The difference between initially withdrawn and the actual cost is refunded.
Expand Down Expand Up @@ -298,7 +303,7 @@ where
// Actual fee after evm execution, including tip.
actual_fee,
// Base fee.
executor.fee(base_fee),
actual_base_fee,
// Fee initially withdrawn.
fee,
);
Expand Down
13 changes: 7 additions & 6 deletions template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use sp_runtime::{
create_runtime_str, generic, impl_opaque_keys,
traits::{
BlakeTwo256, Block as BlockT, DispatchInfoOf, Dispatchable, Get, IdentifyAccount,
IdentityLookup, NumberFor, PostDispatchInfoOf, UniqueSaturatedInto, Verify,
IdentityLookup, NumberFor, One, PostDispatchInfoOf, UniqueSaturatedInto, Verify,
},
transaction_validity::{TransactionSource, TransactionValidity, TransactionValidityError},
ApplyExtrinsicResult, ConsensusEngineId, Perbill, Permill,
Expand All @@ -35,13 +35,13 @@ use frame_support::weights::constants::ParityDbWeight as RuntimeDbWeight;
use frame_support::weights::constants::RocksDbWeight as RuntimeDbWeight;
use frame_support::{
construct_runtime, parameter_types,
traits::{ConstBool, ConstU32, ConstU8, FindAuthor, OnFinalize, OnTimestampSet},
traits::{ConstU32, ConstU32, ConstU8, FindAuthor, OnFinalize, OnTimestampSet},
weights::{constants::WEIGHT_REF_TIME_PER_MILLIS, ConstantMultiplier, IdentityFee, Weight},
};
use pallet_grandpa::{
fg_primitives, AuthorityId as GrandpaId, AuthorityList as GrandpaAuthorityList,
};
use pallet_transaction_payment::CurrencyAdapter;
use pallet_transaction_payment::{ConstFeeMultiplier, CurrencyAdapter};
// Frontier
use fp_account::EthereumSignature;
use fp_evm::weight_per_gas;
Expand All @@ -56,6 +56,7 @@ use pallet_evm::{
pub use frame_system::Call as SystemCall;
pub use pallet_balances::Call as BalancesCall;
pub use pallet_timestamp::Call as TimestampCall;
use pallet_transaction_payment::Multiplier;

mod precompiles;
use precompiles::FrontierPrecompiles;
Expand Down Expand Up @@ -282,16 +283,16 @@ impl pallet_balances::Config for Runtime {
}

parameter_types! {
pub const TransactionByteFee: Balance = 1;
pub FeeMultiplier: Multiplier = Multiplier::one();
}

impl pallet_transaction_payment::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type OnChargeTransaction = CurrencyAdapter<Balances, ()>;
type OperationalFeeMultiplier = ConstU8<5>;
type WeightToFee = IdentityFee<Balance>;
type LengthToFee = ConstantMultiplier<Balance, TransactionByteFee>;
type FeeMultiplierUpdate = ();
type LengthToFee = IdentityFee<Balance>;
type FeeMultiplierUpdate = ConstFeeMultiplier<FeeMultiplier>;
}

impl pallet_sudo::Config for Runtime {
Expand Down

0 comments on commit f36ce90

Please sign in to comment.