Skip to content

Commit

Permalink
introduce new fee calculator for evm domain and refactor eth constant…
Browse files Browse the repository at this point in the history
…s to primitives
  • Loading branch information
vedhavyas committed Mar 11, 2025
1 parent 7f51ed8 commit 76111a1
Show file tree
Hide file tree
Showing 16 changed files with 186 additions and 260 deletions.
20 changes: 4 additions & 16 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ num_cpus = "1.16.0"
ouroboros = "0.18.4"
pallet-auto-id = { version = "0.1.0", path = "domains/pallets/auto-id", default-features = false }
pallet-balances = { git = "https://github.com/subspace/polkadot-sdk", rev = "949bb3cfb64ab974e4fc49328fb3e81c96bc0fbe", default-features = false }
pallet-base-fee = { version = "1.0.0-dev", git = "https://github.com/autonomys/frontier", rev = "986eb1ad6ec69c16d05d142b7e731b4b69e3b409", default-features = false }
pallet-block-fees = { version = "0.1.0", path = "domains/pallets/block-fees", default-features = false }
pallet-collective = { git = "https://github.com/subspace/polkadot-sdk", rev = "949bb3cfb64ab974e4fc49328fb3e81c96bc0fbe", default-features = false }
pallet-democracy = { git = "https://github.com/subspace/polkadot-sdk", rev = "949bb3cfb64ab974e4fc49328fb3e81c96bc0fbe", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion crates/subspace-runtime-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ parameter_types! {

/// Parameterized slow adjusting fee updated based on
/// <https://research.web3.foundation/Polkadot/overview/token-economics#2-slow-adjusting-mechanism>
pub type SlowAdjustingFeeUpdate<R> = TargetedFeeAdjustment<
pub type SlowAdjustingFeeUpdate<R, TargetBlockFullness> = TargetedFeeAdjustment<
R,
TargetBlockFullness,
AdjustmentVariable,
Expand Down
8 changes: 4 additions & 4 deletions crates/subspace-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ use subspace_core_primitives::{PublicKey, Randomness, SlotNumber, U256};
use subspace_runtime_primitives::utility::{DefaultNonceProvider, MaybeIntoUtilityCall};
use subspace_runtime_primitives::{
maximum_normal_block_length, AccountId, Balance, BlockNumber, FindBlockRewardAddress, Hash,
HoldIdentifier, Moment, Nonce, Signature, SlowAdjustingFeeUpdate, BLOCK_WEIGHT_FOR_2_SEC,
DOMAINS_BLOCK_PRUNING_DEPTH, MAX_BLOCK_LENGTH, MIN_REPLICATION_FACTOR, NORMAL_DISPATCH_RATIO,
SHANNON, SLOT_PROBABILITY, SSC,
HoldIdentifier, Moment, Nonce, Signature, SlowAdjustingFeeUpdate, TargetBlockFullness,
BLOCK_WEIGHT_FOR_2_SEC, DOMAINS_BLOCK_PRUNING_DEPTH, MAX_BLOCK_LENGTH, MIN_REPLICATION_FACTOR,
NORMAL_DISPATCH_RATIO, SHANNON, SLOT_PROBABILITY, SSC,
};

sp_runtime::impl_opaque_keys! {
Expand Down Expand Up @@ -399,7 +399,7 @@ impl pallet_transaction_payment::Config for Runtime {
type OperationalFeeMultiplier = ConstU8<5>;
type WeightToFee = ConstantMultiplier<Balance, TransactionWeightFee>;
type LengthToFee = ConstantMultiplier<Balance, TransactionByteFee>;
type FeeMultiplierUpdate = SlowAdjustingFeeUpdate<Runtime>;
type FeeMultiplierUpdate = SlowAdjustingFeeUpdate<Runtime, TargetBlockFullness>;
type WeightInfo = pallet_transaction_payment::weights::SubstrateWeight<Runtime>;
}

Expand Down
4 changes: 4 additions & 0 deletions domains/pallets/evm-tracker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ frame-support.workspace = true
frame-system.workspace = true
pallet-ethereum.workspace = true
pallet-evm.workspace = true
pallet-block-fees.workspace = true
pallet-transaction-payment.workspace = true
pallet-utility.workspace = true
scale-info = { workspace = true, features = ["derive"] }
sp-core.workspace = true
Expand All @@ -37,6 +39,8 @@ std = [
"frame-system/std",
"pallet-ethereum/std",
"pallet-evm/std",
"pallet-block-fees/std",
"pallet-transaction-payment/std",
"pallet-utility/std",
"scale-info/std",
"sp-core/std",
Expand Down
61 changes: 61 additions & 0 deletions domains/pallets/evm-tracker/src/fees.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//! Fees module for EVM domain
use crate::Config;
use core::marker::PhantomData;
use domain_runtime_primitives::Balance;
use pallet_block_fees::Pallet as BlockFees;
use pallet_evm::FeeCalculator;
use pallet_transaction_payment::Pallet as TransactionPayment;
use sp_core::U256;
use sp_evm_tracker::WEIGHT_PER_GAS;
use sp_runtime::traits::Get;
use sp_runtime::{FixedPointNumber, Perbill};
use sp_weights::Weight;

/// Evm gas price calculator for EVM domains.
/// TransactionWeightFee is the fee for 1 unit of Weight.
/// GasPerByte is the gas for 1 byte
pub struct EvmGasPriceCalculator<T, TransactionWeightFee, GasPerByte, StorageFeePercent>(
PhantomData<(T, TransactionWeightFee, GasPerByte, StorageFeePercent)>,
);

impl<T, TransactionWeightFee, GasPerByte, StorageFeePercent> FeeCalculator
for EvmGasPriceCalculator<T, TransactionWeightFee, GasPerByte, StorageFeePercent>
where
T: Config
+ frame_system::Config
+ pallet_transaction_payment::Config
+ pallet_block_fees::Config<Balance = Balance>,
TransactionWeightFee: Get<T::Balance>,
GasPerByte: Get<T::Balance>,
{
fn min_gas_price() -> (U256, Weight) {
// spread the storage fee across the gas price based on the Gas Per Byte.
let storage_fee_per_gas =
BlockFees::<T>::final_domain_transaction_byte_fee().saturating_div(GasPerByte::get());
// adjust the fee per weight using the multiplier
let weight_fee = TransactionWeightFee::get().saturating_mul(WEIGHT_PER_GAS.into());
let adjusted_weight_fee =
TransactionPayment::<T>::next_fee_multiplier().saturating_mul_int(weight_fee);

// finally add the storage_fee_per_gas and adjusted_weight_fee to calculate the final
// min_gas_price.
let min_gas_price = adjusted_weight_fee.saturating_add(storage_fee_per_gas);
(
min_gas_price.into(),
<T as frame_system::Config>::DbWeight::get().reads(2),
)
}
}

impl<T, TransactionWeightFee, BytePerFee, StorageFeePercent>
EvmGasPriceCalculator<T, TransactionWeightFee, BytePerFee, StorageFeePercent>
where
StorageFeePercent: Get<Perbill>,
{
pub fn split_fee_into_storage_and_execution(fee: Balance) -> (Balance, Balance) {
let ratio = StorageFeePercent::get();
let storage_fee = ratio.mul_ceil(fee);
(storage_fee, fee.saturating_sub(storage_fee))
}
}
1 change: 1 addition & 0 deletions domains/pallets/evm-tracker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ extern crate alloc;

pub mod check_nonce;
pub mod create_contract;
pub mod fees;
pub mod traits;

pub use check_nonce::CheckNonce;
Expand Down
8 changes: 6 additions & 2 deletions domains/primitives/evm-tracker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,23 @@ include = [

[dependencies]
async-trait = { workspace = true, optional = true }
parity-scale-codec = { workspace = true, features = ["derive"] }
domain-runtime-primitives.workspace = true
frame-support.workspace = true
parity-scale-codec = { workspace = true, features = ["derive"] }
sp-api.workspace = true
sp-domains.workspace = true
sp-inherents.workspace = true
sp-weights.workspace = true

[features]
default = ["std"]
std = [
"async-trait",
"parity-scale-codec/std",
"domain-runtime-primitives/std",
"frame-support/std",
"parity-scale-codec/std",
"sp-api/std",
"sp-domains/std",
"sp-inherents/std",
"sp-weights/std"
]
30 changes: 29 additions & 1 deletion domains/primitives/evm-tracker/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,40 @@
//! Inherents for EVM tracker
#![cfg_attr(not(feature = "std"), no_std)]

use domain_runtime_primitives::EthereumAccountId;
use domain_runtime_primitives::{maximum_domain_block_weight, Balance, EthereumAccountId};
use frame_support::parameter_types;
use frame_support::sp_runtime::app_crypto::sp_core::U256;
use frame_support::sp_runtime::Perbill;
use parity_scale_codec::{Decode, Encode};
use sp_domains::PermissionedActionAllowedBy;
#[cfg(feature = "std")]
use sp_inherents::{Error, InherentData};
use sp_inherents::{InherentIdentifier, IsFatalError};
use sp_weights::constants::WEIGHT_REF_TIME_PER_SECOND;
use sp_weights::Weight;

/// Current approximation of the gas/s consumption considering
/// EVM execution over compiled WASM (on 4.4Ghz CPU).
pub const GAS_PER_SECOND: u64 = 40_000_000;

/// Approximate ratio of the amount of Weight per Gas.
/// u64 works for approximations because Weight is a very small unit compared to gas.
pub const WEIGHT_PER_GAS: u64 = WEIGHT_REF_TIME_PER_SECOND.saturating_div(GAS_PER_SECOND);

parameter_types! {
pub const GasLimitPovSizeRatio: u64 = 4;
/// Gas per byte
/// Ethereum’s Yellow Paper states that it costs 20,000 gas to store one 256-bit word.
/// 1 Byte costs 20_000/32 = 625
pub const GasPerByte: Balance = 625;
/// Proportion of final (gas_price * gas_used) given as storage fee.
pub const StorageFeeRatio: Perbill = Perbill::from_percent(30);
/// EVM block gas limit is set to maximum to allow all the transaction stored on Consensus chain.
pub BlockGasLimit: U256 = U256::from(
maximum_domain_block_weight().ref_time() / WEIGHT_PER_GAS
);
pub WeightPerGas: Weight = Weight::from_parts(WEIGHT_PER_GAS, 0);
}

/// Executive inherent identifier.
pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"dmnevmtr";
Expand Down
9 changes: 8 additions & 1 deletion domains/primitives/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ use frame_system::limits::{BlockLength, BlockWeights};
use parity_scale_codec::{Decode, Encode};
use scale_info::TypeInfo;
use serde::{Deserialize, Serialize};
use sp_core::parameter_types;
use sp_runtime::generic::{ExtensionVersion, Preamble, UncheckedExtrinsic};
use sp_runtime::traits::transaction_extension::TransactionExtension;
use sp_runtime::traits::{Convert, Dispatchable, IdentifyAccount, Verify};
use sp_runtime::transaction_validity::TransactionValidityError;
use sp_runtime::{MultiAddress, MultiSignature, Perbill};
use sp_runtime::{MultiAddress, MultiSignature, Perbill, Perquintill};
use sp_weights::constants::WEIGHT_REF_TIME_PER_SECOND;
use sp_weights::Weight;
pub use subspace_runtime_primitives::HoldIdentifier;
Expand Down Expand Up @@ -129,6 +130,12 @@ pub const DEFAULT_EXTENSION_VERSION: ExtensionVersion = 0;
/// Storage cost per year of (12 * 1e-9 * 0.1 ) - SSD storage on cloud hosting costs about 0.1 USD per Gb per month
pub const EXISTENTIAL_DEPOSIT: Balance = 1_000_000_000_000 * SHANNON;

parameter_types! {
/// The portion of the `NORMAL_DISPATCH_RATIO` that we adjust the fees with. Blocks filled less
/// than this will decrease the weight and more will increase.
pub const TargetBlockFullness: Perquintill = Perquintill::from_percent(25);
}

/// We assume that ~5% of the block weight is consumed by `on_initialize` handlers. This is
/// used to limit the maximal weight of a single extrinsic.
const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(5);
Expand Down
4 changes: 2 additions & 2 deletions domains/runtime/auto-id/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub use domain_runtime_primitives::{
};
use domain_runtime_primitives::{
AccountId, Address, CheckExtrinsicsValidityError, DecodeExtrinsicError, HoldIdentifier,
Signature, ERR_BALANCE_OVERFLOW, SLOT_DURATION,
Signature, TargetBlockFullness, ERR_BALANCE_OVERFLOW, SLOT_DURATION,
};
use frame_support::dispatch::{DispatchClass, DispatchInfo, GetDispatchInfo};
use frame_support::genesis_builder_helper::{build_state, get_preset};
Expand Down Expand Up @@ -260,7 +260,7 @@ impl pallet_transaction_payment::Config for Runtime {
type OnChargeTransaction = OnChargeDomainTransaction<Balances>;
type WeightToFee = ConstantMultiplier<Balance, TransactionWeightFee>;
type LengthToFee = ConstantMultiplier<Balance, FinalDomainTransactionByteFee>;
type FeeMultiplierUpdate = SlowAdjustingFeeUpdate<Runtime>;
type FeeMultiplierUpdate = SlowAdjustingFeeUpdate<Runtime, TargetBlockFullness>;
type OperationalFeeMultiplier = OperationalFeeMultiplier;
type WeightInfo = pallet_transaction_payment::weights::SubstrateWeight<Runtime>;
}
Expand Down
2 changes: 0 additions & 2 deletions domains/runtime/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ frame-system.workspace = true
frame-system-benchmarking = { workspace = true, optional = true }
frame-system-rpc-runtime-api.workspace = true
pallet-balances.workspace = true
pallet-base-fee.workspace = true
pallet-block-fees.workspace = true
pallet-domain-id.workspace = true
pallet-domain-sudo.workspace = true
Expand Down Expand Up @@ -92,7 +91,6 @@ std = [
"frame-system/std",
"frame-system-rpc-runtime-api/std",
"pallet-balances/std",
"pallet-base-fee/std",
"pallet-domain-id/std",
"pallet-domain-sudo/std",
"pallet-block-fees/std",
Expand Down
Loading

0 comments on commit 76111a1

Please sign in to comment.