Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
saiintbrisson committed Jan 8, 2025
1 parent 429277f commit 0de029c
Show file tree
Hide file tree
Showing 7 changed files with 477 additions and 467 deletions.
815 changes: 415 additions & 400 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ fp-dynamic-fee = { git = "https://github.com/paritytech/frontier.git", rev = "b9
fp-evm = { git = "https://github.com/paritytech/frontier.git", rev = "b9b1c620c8b418bdeeadc79725f9cfa4703c0333", default-features = false }
fp-rpc = { git = "https://github.com/paritytech/frontier.git", rev = "b9b1c620c8b418bdeeadc79725f9cfa4703c0333", default-features = false }
fp-self-contained = { git = "https://github.com/paritytech/frontier.git", rev = "b9b1c620c8b418bdeeadc79725f9cfa4703c0333", default-features = false }
pallet-base-fee = { git = "https://github.com/paritytech/frontier.git", rev = "b9b1c620c8b418bdeeadc79725f9cfa4703c0333", default-features = false }
pallet-ethereum = { git = "https://github.com/paritytech/frontier.git", rev = "b9b1c620c8b418bdeeadc79725f9cfa4703c0333", default-features = false }
pallet-evm = { git = "https://github.com/paritytech/frontier.git", rev = "b9b1c620c8b418bdeeadc79725f9cfa4703c0333", default-features = false }
pallet-evm-chain-id = { git = "https://github.com/paritytech/frontier.git", rev = "b9b1c620c8b418bdeeadc79725f9cfa4703c0333", default-features = false }
Expand Down
7 changes: 5 additions & 2 deletions node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ pub fn development_config() -> Result<ChainSpec, String> {
.with_id("torus-devnet")
.with_protocol_id("torus")
.with_chain_type(ChainType::Development)
.with_genesis_config_patch(testnet_genesis())
.with_genesis_config_patch(devnet_genesis())
.with_properties(props())
.build())
}

/// Configure initial storage state for FRAME pallets.
fn testnet_genesis() -> Value {
fn devnet_genesis() -> Value {
use polkadot_sdk::{
polkadot_sdk_frame::traits::Get,
sp_keyring::{Ed25519Keyring, Sr25519Keyring},
Expand Down Expand Up @@ -75,5 +75,8 @@ fn testnet_genesis() -> Value {
"grandpa": {
"authorities": grandpa.iter().map(|x| (x.public().to_string(), 1)).collect::<Vec<_>>(),
},
"evmChainId": {
"chainId": 21001,
}
})
}
2 changes: 1 addition & 1 deletion node/src/cli/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub struct EthConfiguration {

/// Maximum allowed gas limit will be `block.gas_limit * execute_gas_limit_multiplier`
/// when using eth_call/eth_estimateGas.
#[arg(long, default_value = "2")]
#[arg(long, default_value = "10")]
pub execute_gas_limit_multiplier: u64,
/// Size in bytes of the LRU cache for block data.
#[arg(long, default_value = "50")]
Expand Down
2 changes: 2 additions & 0 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ std = [
"fp-evm/std",
"fp-rpc/std",
"fp-self-contained/std",
"pallet-base-fee/std",
"pallet-ethereum/std",
"pallet-evm/std",
"pallet-evm-chain-id/std",
Expand Down Expand Up @@ -76,6 +77,7 @@ polkadot-sdk = { workspace = true, features = [
fp-evm = { workspace = true, features = ["serde"] }
fp-rpc.workspace = true
fp-self-contained = { workspace = true, features = ["serde"] }
pallet-base-fee.workspace = true
pallet-ethereum.workspace = true
pallet-evm.workspace = true
pallet-evm-chain-id.workspace = true
Expand Down
111 changes: 47 additions & 64 deletions runtime/src/configs/eth.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
use crate::{
configs::{currency, WEIGHT_REF_TIME_PER_SECOND},
TransactionPayment,
};
use codec::{Decode, Encode};
use fp_evm::weight_per_gas;
use pallet_ethereum::PostLogContent;
use pallet_evm::{FeeCalculator, HashedAddressMapping};
use pallet_evm::HashedAddressMapping;
use polkadot_sdk::{
frame_support::{parameter_types, traits::FindAuthor},
frame_system, pallet_aura,
pallet_aura,
polkadot_sdk_frame::prelude::{TransactionValidity, TransactionValidityError},
sp_core::{ConstU32, H160, U256},
sp_runtime::{
traits::{BlakeTwo256, Block as BlockT, DispatchInfoOf, Dispatchable, PostDispatchInfoOf},
ConsensusEngineId, DispatchResultWithInfo, FixedPointNumber, RuntimeAppPublic,
ConsensusEngineId, DispatchResultWithInfo, Permill, RuntimeAppPublic,
},
sp_weights::Weight,
};
Expand All @@ -39,83 +36,69 @@ impl<F: FindAuthor<u32>> FindAuthor<H160> for FindAuthorTruncated<F> {
}
}

/// Current approximation of the gas/s consumption considering
/// EVM execution over compiled WASM (on 4.4Ghz CPU).
/// Given the 2000ms Weight, from which 75% only are used for transactions,
/// the total EVM execution gas limit is: GAS_PER_SECOND * 2 * 0.75 ~= 60_000_000.
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 / GAS_PER_SECOND;
parameter_types! {
pub DefaultBaseFeePerGas: U256 = U256::from(1_000_000_000);
pub DefaultElasticity: Permill = Permill::from_parts(125_000);
}
pub struct BaseFeeThreshold;
impl pallet_base_fee::BaseFeeThreshold for BaseFeeThreshold {
fn lower() -> Permill {
Permill::zero()
}
fn ideal() -> Permill {
Permill::from_parts(500_000)
}
fn upper() -> Permill {
Permill::from_parts(1_000_000)
}
}
impl pallet_base_fee::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Threshold = BaseFeeThreshold;
type DefaultBaseFeePerGas = DefaultBaseFeePerGas;
type DefaultElasticity = DefaultElasticity;
}

const BLOCK_GAS_LIMIT: u64 = 75_000_000;
const MAX_POV_SIZE: u64 = 5 * 1024 * 1024;

/// Maximum weight per block
pub const MAXIMUM_BLOCK_WEIGHT: Weight = Weight::from_parts(WEIGHT_REF_TIME_PER_SECOND, u64::MAX)
.saturating_mul(2)
.set_proof_size(MAX_POV_SIZE);
/// The maximum storage growth per block in bytes.
const MAX_STORAGE_GROWTH: u64 = 400 * 1024;
/// Allow 1/4 of the block time.
const WEIGHT_MILLISECS_PER_BLOCK: u64 = 8000 / 4;

parameter_types! {
pub BlockGasLimit: U256
= U256::from(NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT.ref_time() / WEIGHT_PER_GAS);
pub const GasLimitPovSizeRatio: u64 = 16;
/// The amount of gas per storage (in bytes): BLOCK_GAS_LIMIT / BLOCK_STORAGE_LIMIT
/// The current definition of BLOCK_STORAGE_LIMIT is 160 KB, resulting in a value of 366.
pub GasLimitStorageGrowthRatio: u64 = 366;
pub BlockGasLimit: U256 = U256::from(BLOCK_GAS_LIMIT);
pub const GasLimitPovSizeRatio: u64 = BLOCK_GAS_LIMIT.saturating_div(MAX_POV_SIZE);
pub const GasLimitStorageGrowthRatio: u64 = BLOCK_GAS_LIMIT.saturating_div(MAX_STORAGE_GROWTH);
pub PrecompilesValue: FrontierPrecompiles = FrontierPrecompiles;
pub EthereumChainId: u64 = 21000;
pub WeightPerGas: Weight = Weight::from_parts(WEIGHT_PER_GAS, 0);
}

pub struct TransactionPaymentAsGasPrice;
impl FeeCalculator for TransactionPaymentAsGasPrice {
fn min_gas_price() -> (U256, Weight) {
// note: transaction-payment differs from EIP-1559 in that its tip and length fees are not
// scaled by the multiplier, which means its multiplier will be overstated when
// applied to an ethereum transaction
// note: transaction-payment uses both a congestion modifier (next_fee_multiplier, which is
// updated once per block in on_finalize) and a 'WeightToFee' implementation. Our
// runtime implements this as a 'ConstantModifier', so we can get away with a simple
// multiplication here.
// It is imperative that `saturating_mul_int` be performed as late as possible in the
// expression since it involves fixed point multiplication with a division by a fixed
// divisor. This leads to truncation and subsequent precision loss if performed too early.
// This can lead to min_gas_price being same across blocks even if the multiplier changes.
// There's still some precision loss when the final `gas_price` (used_gas * min_gas_price)
// is computed in frontier, but that's currently unavoidable.
let min_gas_price = TransactionPayment::next_fee_multiplier()
.saturating_mul_int((currency::WEIGHT_FEE).saturating_mul(WEIGHT_PER_GAS as u128));
(
min_gas_price.into(),
<Runtime as frame_system::Config>::DbWeight::get().reads(1),
)
}
pub WeightPerGas: Weight = Weight::from_parts(weight_per_gas(BLOCK_GAS_LIMIT, NORMAL_DISPATCH_RATIO, WEIGHT_MILLISECS_PER_BLOCK), 0);
pub SuicideQuickClearLimit: u32 = 0;
// pub EthereumChainId: u64 = 21000;
}

impl pallet_evm::Config for Runtime {
type FeeCalculator = TransactionPaymentAsGasPrice;
type AccountProvider = pallet_evm::FrameSystemAccountProvider<Self>;
type FeeCalculator = super::BaseFee;
type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
type WeightPerGas = WeightPerGas; // todo: check
type WeightPerGas = WeightPerGas;
type BlockHashMapping = pallet_ethereum::EthereumBlockHashMapping<Self>;
type CallOrigin = pallet_evm::EnsureAddressTruncated;
type WithdrawOrigin = pallet_evm::EnsureAddressTruncated;
type AddressMapping = HashedAddressMapping<BlakeTwo256>;
type Currency = Balances;
type RuntimeEvent = RuntimeEvent;
type Runner = pallet_evm::runner::stack::Runner<Self>;
type PrecompilesType = FrontierPrecompiles;
type PrecompilesValue = PrecompilesValue;
type ChainId = EthereumChainId;
type OnChargeTransaction = ();
type ChainId = crate::EvmChainId;
type BlockGasLimit = BlockGasLimit;
type FindAuthor = FindAuthorTruncated<Aura>;
type Runner = pallet_evm::runner::stack::Runner<Self>;
type OnChargeTransaction = ();
type OnCreate = ();
type GasLimitPovSizeRatio = GasLimitPovSizeRatio; // todo: check
type SuicideQuickClearLimit = ConstU32<0>;
type GasLimitStorageGrowthRatio = GasLimitStorageGrowthRatio; // todo: check
type FindAuthor = FindAuthorTruncated<Aura>;
type GasLimitPovSizeRatio = GasLimitPovSizeRatio;
type SuicideQuickClearLimit = SuicideQuickClearLimit;
type GasLimitStorageGrowthRatio = GasLimitStorageGrowthRatio;
type Timestamp = Timestamp;
type AccountProvider = pallet_evm::FrameSystemAccountProvider<Self>;
type WeightInfo = pallet_evm::weights::SubstrateWeight<Self>;
}

Expand Down
6 changes: 6 additions & 0 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ mod runtime {
#[runtime::pallet_index(9)]
pub type EVM = pallet_evm::Pallet<Runtime>;

#[runtime::pallet_index(14)]
pub type BaseFee = pallet_base_fee::Pallet<Runtime>;

#[runtime::pallet_index(15)]
pub type EvmChainId = pallet_evm_chain_id::Pallet<Runtime>;

#[runtime::pallet_index(11)]
pub type Governance = pallet_governance::Pallet<Runtime>;

Expand Down

0 comments on commit 0de029c

Please sign in to comment.