Skip to content

Commit

Permalink
Use transaction-payment's congestion modifier for Ethereum base-fee (
Browse files Browse the repository at this point in the history
…moonbeam-foundation#1765)

* Convert transaction-payments congestion modifier to replace Ethereum's base-fee

* Remove outdated comment

* fix precision bug

* import

* use saturating mul, improve doc

* update fee parameters, add tests

* make tests similar

* add tests to all runtimes

* fix tests

* Add transaction-payment GenesisConfig to initialize its Multiplier

* Fix some moonbase tests

* Rename FixedGasPrice -> TransactionPaymentAsGasPrice

* fmt

* More ts test fixes

* Fixes staking locks

* Fix existential balance tests

* Fix length fee tests

* Query base_fee in createTransaction when needed

* Fixes gasPrice for existential test

* Fix deposit/fee check for multiple deposit

* Fix merge issue

* Use legacy txns for some tests that expect full gas_price to be charged

* Bump gas price for tests

* Update tests to reflect createTransfer default

* Use constant for DEFAULT_TXN_MAX_BASE_FEE

* Reflect txn values to reflect base_fee change

* Prefer constant over literal value

* Overhaul fee calculations in verifyBlockFees

* Bound effectiveTipPerGas to 0

* Fix substrate-based fees

* Overestimate delegation count weight hint

* Use legacy txns and expect full gas_price to be paid as fee

* Use constant for gas limit value

* Start test case for max possible fee conditions

* Clean up

* Add runtime-upgrade test

* prettier

* First look at max possible fee

* fix auto-compound delegation tx order flakiness

* prettier

* Remove cargo override (oops)

* Hack to fix race condition

* prettier

* Use beforeEach for setting max multiplier

* Test multiplier against Fibonacci contract

* prettier

* prettier

* fmt

* Various minor fixes

* Add some fee multiplier scenarios

* Don't use EXTRINSIC_BASE_WEIGHT in gas calc

* Don't expect genesis value at each beforeAll

* Bump expectation of CREATE cost

* Slightly change length fee assumptions

* Use higher gas price

* Remove base_fee test

* fmt

* toml sort

* Resolve compiler warnings

* More compiler warning fixes

* Fix receipt/status test

* Remove irrelevant tests

* Use maxDelegationCount for weight hint

* Remove comment

* Remove ignored tests

* Move fee tests to integration_tests.rs

* Re-remove ignored test

* Move moonbeam runtime fee tests to integration_tests

* Move moonriver runtime fee tests to integration_tests

* fmt

* Use base fee constant for gas price

* Revert test name change...

Co-authored-by: Nisheeth Barthwal <[email protected]>
Co-authored-by: Crystalin <[email protected]>
Co-authored-by: tgmichel <[email protected]>
  • Loading branch information
4 people authored and imstar15 committed May 16, 2023
1 parent 8a83855 commit 5e434b0
Show file tree
Hide file tree
Showing 42 changed files with 890 additions and 298 deletions.
6 changes: 5 additions & 1 deletion node/service/src/chain_spec/moonbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ use moonbase_runtime::{
EthereumChainIdConfig, EthereumConfig, GenesisAccount, GenesisConfig, InflationInfo,
MaintenanceModeConfig, ParachainInfoConfig, ParachainStakingConfig, PolkadotXcmConfig,
Precompiles, Range, SudoConfig, SystemConfig, TechCommitteeCollectiveConfig,
TreasuryCouncilCollectiveConfig, HOURS, WASM_BINARY,
TransactionPaymentConfig, TreasuryCouncilCollectiveConfig, HOURS, WASM_BINARY,
};
use nimbus_primitives::NimbusId;
use pallet_transaction_payment::Multiplier;
use sc_service::ChainType;
#[cfg(test)]
use sp_core::ecdsa;
Expand Down Expand Up @@ -321,6 +322,9 @@ pub fn testnet_genesis(
},
// This should initialize it to whatever we have set in the pallet
polkadot_xcm: PolkadotXcmConfig::default(),
transaction_payment: TransactionPaymentConfig {
multiplier: Multiplier::from(8u128),
},
}
}

Expand Down
6 changes: 5 additions & 1 deletion node/service/src/chain_spec/moonbeam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ use moonbeam_runtime::{
Balance, BalancesConfig, CouncilCollectiveConfig, CrowdloanRewardsConfig, DemocracyConfig,
EVMConfig, EthereumChainIdConfig, EthereumConfig, GenesisAccount, GenesisConfig, InflationInfo,
MaintenanceModeConfig, ParachainInfoConfig, ParachainStakingConfig, PolkadotXcmConfig,
Precompiles, Range, SystemConfig, TechCommitteeCollectiveConfig,
Precompiles, Range, SystemConfig, TechCommitteeCollectiveConfig, TransactionPaymentConfig,
TreasuryCouncilCollectiveConfig, HOURS, WASM_BINARY,
};
use nimbus_primitives::NimbusId;
use pallet_transaction_payment::Multiplier;
use sc_service::ChainType;
#[cfg(test)]
use sp_core::ecdsa;
Expand Down Expand Up @@ -313,6 +314,9 @@ pub fn testnet_genesis(
},
// This should initialize it to whatever we have set in the pallet
polkadot_xcm: PolkadotXcmConfig::default(),
transaction_payment: TransactionPaymentConfig {
multiplier: Multiplier::from(8u128),
},
}
}

Expand Down
6 changes: 5 additions & 1 deletion node/service/src/chain_spec/moonriver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ use moonriver_runtime::{
CouncilCollectiveConfig, CrowdloanRewardsConfig, DemocracyConfig, EVMConfig,
EthereumChainIdConfig, EthereumConfig, GenesisAccount, GenesisConfig, InflationInfo,
MaintenanceModeConfig, ParachainInfoConfig, ParachainStakingConfig, PolkadotXcmConfig,
Precompiles, Range, SystemConfig, TechCommitteeCollectiveConfig,
Precompiles, Range, SystemConfig, TechCommitteeCollectiveConfig, TransactionPaymentConfig,
TreasuryCouncilCollectiveConfig, HOURS, WASM_BINARY,
};
use nimbus_primitives::NimbusId;
use pallet_transaction_payment::Multiplier;
use sc_service::ChainType;
#[cfg(test)]
use sp_core::ecdsa;
Expand Down Expand Up @@ -313,6 +314,9 @@ pub fn testnet_genesis(
},
// This should initialize it to whatever we have set in the pallet
polkadot_xcm: PolkadotXcmConfig::default(),
transaction_payment: TransactionPaymentConfig {
multiplier: Multiplier::from(8u128),
},
}
}

Expand Down
37 changes: 23 additions & 14 deletions runtime/moonbase/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ pub fn native_version() -> NativeVersion {
}

const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75);
const NORMAL_WEIGHT: Weight = MAXIMUM_BLOCK_WEIGHT.saturating_mul(3).saturating_div(4);
pub const NORMAL_WEIGHT: Weight = MAXIMUM_BLOCK_WEIGHT.saturating_mul(3).saturating_div(4);
// Here we assume Ethereum's base fee of 21000 gas and convert to weight, but we
// subtract roughly the cost of a balance transfer from it (about 1/3 the cost)
// and some cost to account for per-byte-fee.
Expand Down Expand Up @@ -388,27 +388,37 @@ parameter_types! {
pub const TargetBlockFullness: Perquintill = Perquintill::from_percent(25);
/// The adjustment variable of the runtime. Higher values will cause `TargetBlockFullness` to
/// change the fees more rapidly. This low value causes changes to occur slowly over time.
pub AdjustmentVariable: Multiplier = Multiplier::saturating_from_rational(3, 100_000);
pub AdjustmentVariable: Multiplier = Multiplier::saturating_from_rational(4, 1_000);
/// Minimum amount of the multiplier. This value cannot be too low. A test case should ensure
/// that combined with `AdjustmentVariable`, we can recover from the minimum.
/// See `multiplier_can_grow_from_zero` in integration_tests.rs.
/// This value is currently only used by pallet-transaction-payment as an assertion that the
/// next multiplier is always > min value.
pub MinimumMultiplier: Multiplier = Multiplier::saturating_from_rational(1, 1_000_000u128);
pub MinimumMultiplier: Multiplier = Multiplier::saturating_from_rational(1, 1_000);
/// Maximum multiplier. We pick a value that is expensive but not impossibly so; it should act
/// as a safety net.
pub MaximumMultiplier: Multiplier = Multiplier::from(100_000u128);
pub PrecompilesValue: MoonbasePrecompiles<Runtime> = MoonbasePrecompiles::<_>::new();
pub WeightPerGas: Weight = Weight::from_ref_time(WEIGHT_PER_GAS);
}

pub struct FixedGasPrice;
impl FeeCalculator for FixedGasPrice {
pub struct TransactionPaymentAsGasPrice;
impl FeeCalculator for TransactionPaymentAsGasPrice {
fn min_gas_price() -> (U256, Weight) {
(
(1 * currency::GIGAWEI * currency::SUPPLY_FACTOR).into(),
Weight::zero(),
)
// TODO: 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(), Weight::zero())
}
}

Expand Down Expand Up @@ -451,7 +461,7 @@ where
moonbeam_runtime_common::impl_on_charge_evm_transaction!();

impl pallet_evm::Config for Runtime {
type FeeCalculator = FixedGasPrice;
type FeeCalculator = TransactionPaymentAsGasPrice;
type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
type WeightPerGas = WeightPerGas;
type BlockHashMapping = pallet_ethereum::EthereumBlockHashMapping<Self>;
Expand Down Expand Up @@ -1250,7 +1260,7 @@ construct_runtime! {
Sudo: pallet_sudo::{Pallet, Call, Config<T>, Storage, Event<T>} = 4,
RandomnessCollectiveFlip: pallet_randomness_collective_flip::{Pallet, Storage} = 5,
ParachainSystem: cumulus_pallet_parachain_system::{Pallet, Call, Storage, Inherent, Event<T>} = 6,
TransactionPayment: pallet_transaction_payment::{Pallet, Storage, Event<T>} = 7,
TransactionPayment: pallet_transaction_payment::{Pallet, Storage, Config, Event<T>} = 7,
ParachainInfo: parachain_info::{Pallet, Storage, Config} = 8,
EthereumChainId: pallet_ethereum_chain_id::{Pallet, Storage, Config} = 9,
EVM: pallet_evm::{Pallet, Config, Call, Storage, Event<T>} = 10,
Expand Down Expand Up @@ -1508,7 +1518,6 @@ mod tests {
5_u8
);
assert_eq!(STORAGE_BYTE_FEE, Balance::from(100 * MICROUNIT));
assert_eq!(FixedGasPrice::min_gas_price().0, (1 * GIGAWEI).into());

// democracy minimums
assert_eq!(
Expand Down
21 changes: 15 additions & 6 deletions runtime/moonbase/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ use moonbase_runtime::{asset_config::AssetRegistrarMetadata, xcm_config::AssetTy
pub use moonbase_runtime::{
currency::{GIGAWEI, SUPPLY_FACTOR, UNIT, WEI},
AccountId, AssetId, AssetManager, Assets, AuthorInherent, Balance, Balances, CrowdloanRewards,
Ethereum, Executive, FixedGasPrice, InflationInfo, LocalAssets, ParachainStaking, Range,
Runtime, RuntimeCall, RuntimeEvent, System, TransactionConverter, UncheckedExtrinsic, HOURS,
WEEKS,
Ethereum, Executive, InflationInfo, LocalAssets, ParachainStaking, Range, Runtime, RuntimeCall,
RuntimeEvent, System, TransactionConverter, TransactionPaymentAsGasPrice, UncheckedExtrinsic,
HOURS, WEEKS,
};
use nimbus_primitives::{NimbusId, NIMBUS_ENGINE_ID};
use sp_core::{Encode, H160};
Expand All @@ -38,12 +38,13 @@ use sp_runtime::{Digest, DigestItem, Perbill, Percent};
use std::collections::BTreeMap;

use fp_rpc::ConvertTransaction;
use pallet_transaction_payment::Multiplier;

// A valid signed Alice transfer.
pub const VALID_ETH_TX: &str =
"f86880843b9aca0083b71b0094111111111111111111111111111111111111111182020080820a26a\
08c69faf613b9f72dbb029bb5d5acf42742d214c79743507e75fdc8adecdee928a001be4f58ff278ac\
61125a81a582a717d9c5d6554326c01b878297c6522b12282";
"02f86d8205018085174876e80085e8d4a5100082520894f24ff3a9cf04c71dbc94d0b566f7a27b9456\
6cac8080c001a0e1094e1a52520a75c0255db96132076dd0f1263089f838bea548cbdbfc64a4d19f031c\
92a8cb04e2d68d20a6158d542a07ac440cc8d07b6e36af02db046d92df";

// An invalid signed Alice transfer with a gas limit artifically set to 0.
pub const INVALID_ETH_TX: &str =
Expand Down Expand Up @@ -291,6 +292,14 @@ impl ExtBuilder {
)
.unwrap();

<pallet_transaction_payment::GenesisConfig as GenesisBuild<Runtime>>::assimilate_storage(
&pallet_transaction_payment::GenesisConfig {
multiplier: Multiplier::from(8u128),
},
&mut t,
)
.unwrap();

let mut ext = sp_io::TestExternalities::new(t);

let local_assets = self.local_assets.clone();
Expand Down
Loading

0 comments on commit 5e434b0

Please sign in to comment.