Skip to content

Commit

Permalink
Integrated local
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinonard committed Sep 28, 2023
1 parent 2a60a5c commit 8f616da
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 53 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

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

12 changes: 4 additions & 8 deletions bin/collator/src/local/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
//! Chain specifications.
use local_runtime::{
wasm_binary_unwrap, AccountId, AuraConfig, AuraId, BalancesConfig, BaseFeeConfig,
BlockRewardConfig, CouncilConfig, DemocracyConfig, EVMConfig, GenesisConfig, GrandpaConfig,
GrandpaId, Precompiles, Signature, SudoConfig, SystemConfig, TechnicalCommitteeConfig,
TreasuryConfig, VestingConfig,
wasm_binary_unwrap, AccountId, AuraConfig, AuraId, BalancesConfig, BlockRewardConfig,
CouncilConfig, DemocracyConfig, EVMConfig, GenesisConfig, GrandpaConfig, GrandpaId,
Precompiles, Signature, SudoConfig, SystemConfig, TechnicalCommitteeConfig, TreasuryConfig,
VestingConfig,
};
use sc_service::ChainType;
use sp_core::{crypto::Ss58Codec, sr25519, Pair, Public};
Expand Down Expand Up @@ -154,10 +154,6 @@ fn testnet_genesis(
.collect(),
},
ethereum: Default::default(),
base_fee: BaseFeeConfig::new(
sp_core::U256::from(1_000_000_000u64),
sp_runtime::Permill::zero(),
),
sudo: SudoConfig {
key: Some(root_key),
},
Expand Down
8 changes: 5 additions & 3 deletions runtime/local/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ repository.workspace = true
log = { workspace = true, optional = true }
parity-scale-codec = { workspace = true }
scale-info = { workspace = true }
smallvec = { workspace = true }

fp-rpc = { workspace = true }
fp-self-contained = { workspace = true }
Expand All @@ -20,7 +21,6 @@ frame-system = { workspace = true }
pallet-assets = { workspace = true }
pallet-aura = { workspace = true }
pallet-balances = { workspace = true }
pallet-base-fee = { workspace = true }
pallet-collective = { workspace = true }
pallet-contracts = { workspace = true }
pallet-contracts-primitives = { workspace = true }
Expand Down Expand Up @@ -71,6 +71,7 @@ pallet-chain-extension-dapps-staking = { workspace = true }
pallet-chain-extension-xvm = { workspace = true }
pallet-custom-signatures = { workspace = true }
pallet-dapps-staking = { workspace = true }
pallet-dynamic-evm-base-fee = { workspace = true }
pallet-evm-precompile-assets-erc20 = { workspace = true }
pallet-evm-precompile-dapps-staking = { workspace = true }
pallet-evm-precompile-sr25519 = { workspace = true }
Expand Down Expand Up @@ -117,7 +118,7 @@ std = [
"pallet-chain-extension-xvm/std",
"pallet-custom-signatures/std",
"pallet-dapps-staking/std",
"pallet-base-fee/std",
"pallet-dynamic-evm-base-fee/std",
"pallet-ethereum/std",
"pallet-evm/std",
"pallet-evm-precompile-blake2/std",
Expand Down Expand Up @@ -185,6 +186,7 @@ runtime-benchmarks = [
"pallet-ethereum-checked/runtime-benchmarks",
"astar-primitives/runtime-benchmarks",
"pallet-assets/runtime-benchmarks",
"pallet-dynamic-evm-base-fee/runtime-benchmarks",
]
try-runtime = [
"fp-self-contained/try-runtime",
Expand Down Expand Up @@ -215,7 +217,7 @@ try-runtime = [
"pallet-proxy/try-runtime",
"pallet-treasury/try-runtime",
"pallet-preimage/try-runtime",
"pallet-base-fee/try-runtime",
"pallet-dynamic-evm-base-fee/try-runtime",
"pallet-evm/try-runtime",
"pallet-ethereum-checked/try-runtime",
]
Expand Down
118 changes: 77 additions & 41 deletions runtime/local/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ use frame_support::{
EqualPrivilegeOnly, FindAuthor, Get, InstanceFilter, Nothing, OnFinalize, WithdrawReasons,
},
weights::{
constants::{RocksDbWeight, WEIGHT_REF_TIME_PER_SECOND},
ConstantMultiplier, IdentityFee, Weight,
constants::{ExtrinsicBaseWeight, RocksDbWeight, WEIGHT_REF_TIME_PER_SECOND},
ConstantMultiplier, Weight, WeightToFeeCoefficient, WeightToFeeCoefficients,
WeightToFeePolynomial,
},
ConsensusEngineId, PalletId,
};
Expand All @@ -44,6 +45,7 @@ use pallet_ethereum::PostLogContent;
use pallet_evm::{FeeCalculator, GasWeightMapping, Runner};
use pallet_evm_precompile_assets_erc20::AddressToAssetId;
use pallet_grandpa::{fg_primitives, AuthorityList as GrandpaAuthorityList};
use pallet_transaction_payment::{CurrencyAdapter, Multiplier, TargetedFeeAdjustment};
use parity_scale_codec::{Compact, Decode, Encode, MaxEncodedLen};
use sp_api::impl_runtime_apis;
use sp_core::{crypto::KeyTypeId, ConstBool, OpaqueMetadata, H160, H256, U256};
Expand All @@ -56,7 +58,7 @@ use sp_runtime::{
transaction_validity::{
TransactionPriority, TransactionSource, TransactionValidity, TransactionValidityError,
},
ApplyExtrinsicResult, RuntimeDebug,
ApplyExtrinsicResult, FixedPointNumber, Perbill, Permill, Perquintill, RuntimeDebug,
};
use sp_std::prelude::*;

Expand All @@ -73,12 +75,9 @@ pub use frame_system::Call as SystemCall;
pub use pallet_balances::Call as BalancesCall;
pub use pallet_grandpa::AuthorityId as GrandpaId;
pub use pallet_timestamp::Call as TimestampCall;
use pallet_transaction_payment::CurrencyAdapter;
pub use sp_consensus_aura::sr25519::AuthorityId as AuraId;
#[cfg(any(feature = "std", test))]
pub use sp_runtime::BuildStorage;
pub use sp_runtime::{Perbill, Permill};

#[cfg(feature = "std")]
/// Wasm binary unwrapped. If built with `BUILD_DUMMY_WASM_BINARY`, the function panics.
pub fn wasm_binary_unwrap() -> &'static [u8] {
Expand Down Expand Up @@ -332,18 +331,81 @@ impl pallet_assets::Config for Runtime {
type BenchmarkHelper = astar_primitives::benchmarks::AssetsBenchmarkHelper;
}

// These values are based on the Astar 2.0 Tokenomics Modeling report.
parameter_types! {
pub const TransactionByteFee: Balance = 1;
pub const TransactionLengthFeeFactor: Balance = 23_500_000_000_000; // 0.000_023_500_000_000_000 SBY per byte
pub const WeightFeeFactor: Balance = 30_855_000_000_000_000; // Around 0.03 SBY per unit of ref time.
pub const TargetBlockFullness: Perquintill = Perquintill::from_percent(25);
pub const OperationalFeeMultiplier: u8 = 5;
pub AdjustmentVariable: Multiplier = Multiplier::saturating_from_rational(1, 666_667); // 0.000_015
pub MinimumMultiplier: Multiplier = Multiplier::saturating_from_rational(1, 10); // 0.1
pub MaximumMultiplier: Multiplier = Multiplier::saturating_from_integer(10); // 10
}

/// Handles converting a weight scalar to a fee value, based on the scale and granularity of the
/// node's balance type.
///
/// This should typically create a mapping between the following ranges:
/// - [0, MAXIMUM_BLOCK_WEIGHT]
/// - [Balance::min, Balance::max]
///
/// Yet, it can be used for any other sort of change to weight-fee. Some examples being:
/// - Setting it to `0` will essentially disable the weight fee.
/// - Setting it to `1` will cause the literal `#[weight = x]` values to be charged.
pub struct WeightToFee;
impl WeightToFeePolynomial for WeightToFee {
type Balance = Balance;
fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
let p = WeightFeeFactor::get();
let q = Balance::from(ExtrinsicBaseWeight::get().ref_time());
smallvec::smallvec![WeightToFeeCoefficient {
degree: 1,
negative: false,
coeff_frac: Perbill::from_rational(p % q, q),
coeff_integer: p / q,
}]
}
}

impl pallet_transaction_payment::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type OnChargeTransaction = CurrencyAdapter<Balances, ()>;
type WeightToFee = IdentityFee<Balance>;
type WeightToFee = WeightToFee;
type OperationalFeeMultiplier = OperationalFeeMultiplier;
type FeeMultiplierUpdate = ();
type LengthToFee = ConstantMultiplier<Balance, TransactionByteFee>;
type FeeMultiplierUpdate = TargetedFeeAdjustment<
Self,
TargetBlockFullness,
AdjustmentVariable,
MinimumMultiplier,
MaximumMultiplier,
>;
type LengthToFee = ConstantMultiplier<Balance, TransactionLengthFeeFactor>;
}

parameter_types! {
pub DefaultBaseFeePerGas: U256 = U256::from(1_470_000_000_000_u128);
pub MinBaseFeePerGas: U256 = U256::from(800_000_000_000_u128);
pub MaxBaseFeePerGas: U256 = U256::from(80_000_000_000_000_u128);
pub StepLimitRatio: Perquintill = Perquintill::from_rational(5_u128, 100_000);
}

/// Simple wrapper for fetching current native transaction fee weight fee multiplier.
pub struct AdjustmentFactorGetter;
impl Get<Multiplier> for AdjustmentFactorGetter {
fn get() -> Multiplier {
pallet_transaction_payment::NextFeeMultiplier::<Runtime>::get()
}
}

impl pallet_dynamic_evm_base_fee::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type DefaultBaseFeePerGas = DefaultBaseFeePerGas;
type MinBaseFeePerGas = MinBaseFeePerGas;
type MaxBaseFeePerGas = MaxBaseFeePerGas;
type AdjustmentFactor = AdjustmentFactorGetter;
type WeightFactor = WeightFeeFactor;
type StepLimitRatio = StepLimitRatio;
type WeightInfo = pallet_dynamic_evm_base_fee::weights::SubstrateWeight<Runtime>;
}

parameter_types! {
Expand Down Expand Up @@ -478,33 +540,6 @@ impl pallet_xvm::Config for Runtime {
type WeightInfo = pallet_xvm::weights::SubstrateWeight<Runtime>;
}

parameter_types! {
// Tells `pallet_base_fee` whether to calculate a new BaseFee `on_finalize` or not.
pub DefaultBaseFeePerGas: U256 = (MILLIAST / 1_000_000).into();
// At the moment, we don't use dynamic fee calculation for local chain by default
pub DefaultElasticity: Permill = Permill::zero();
}

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

/// Current approximation of the gas/s consumption considering
/// EVM execution over compiled WASM (on 4.4Ghz CPU).
/// Given the 500ms Weight, from which 75% only are used for transactions,
Expand Down Expand Up @@ -553,7 +588,7 @@ parameter_types! {
}

impl pallet_evm::Config for Runtime {
type FeeCalculator = BaseFee;
type FeeCalculator = DynamicEvmBaseFee;
type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
type WeightPerGas = WeightPerGas;
type BlockHashMapping = pallet_ethereum::EthereumBlockHashMapping<Runtime>;
Expand Down Expand Up @@ -911,7 +946,7 @@ impl InstanceFilter<RuntimeCall> for ProxyType {
// Skip entire EVM pallet
// Skip entire Ethereum pallet
// Skip entire EthCall pallet
| RuntimeCall::BaseFee(..)
| RuntimeCall::DynamicEvmBaseFee(..)
// Skip entire Contracts pallet
| RuntimeCall::Democracy(..)
| RuntimeCall::Council(..)
Expand Down Expand Up @@ -1010,7 +1045,7 @@ construct_runtime!(
EVM: pallet_evm,
Ethereum: pallet_ethereum,
EthCall: pallet_custom_signatures,
BaseFee: pallet_base_fee,
DynamicEvmBaseFee: pallet_dynamic_evm_base_fee,
Contracts: pallet_contracts,
Sudo: pallet_sudo,
Assets: pallet_assets,
Expand Down Expand Up @@ -1137,6 +1172,7 @@ mod benches {
[pallet_dapps_staking, DappsStaking]
[pallet_block_reward, BlockReward]
[pallet_ethereum_checked, EthereumChecked]
[pallet_dynamic_evm_base_fee, DynamicEvmBaseFee]
);
}

Expand Down Expand Up @@ -1529,7 +1565,7 @@ impl_runtime_apis! {
}

fn elasticity() -> Option<Permill> {
Some(pallet_base_fee::Elasticity::<Runtime>::get())
Some(Permill::zero())
}

fn gas_limit_multiplier_support() {}
Expand Down

0 comments on commit 8f616da

Please sign in to comment.