Skip to content

Commit

Permalink
Align GasWeightMapping with Substrate do_pre_dispatch logic (moon…
Browse files Browse the repository at this point in the history
…beam-foundation#1884)

* Align `GasWeightMapping` with Substrate `do_pre_dispatch` logic

* Fix tests attempt 1

* Fix tests attempt 2

* Fix ts tests attempt 1

* Remove consolelog

* Increase gas limit in test to account for extra weight headroom

* Fix test

* prettier

Co-authored-by: notlesh <[email protected]>
Co-authored-by: Crystalin <[email protected]>
  • Loading branch information
3 people authored and imstar15 committed May 16, 2023
1 parent f7bd8c2 commit b083629
Show file tree
Hide file tree
Showing 27 changed files with 118 additions and 75 deletions.
30 changes: 18 additions & 12 deletions pallets/ethereum-xcm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,15 @@ pub mod pallet {
{
/// Xcm Transact an Ethereum transaction.
/// Weight: Gas limit plus the db read involving the suspension check
#[pallet::weight(<T as pallet_evm::Config>::GasWeightMapping::gas_to_weight({
match xcm_transaction {
EthereumXcmTransaction::V1(v1_tx) => v1_tx.gas_limit.unique_saturated_into(),
EthereumXcmTransaction::V2(v2_tx) => v2_tx.gas_limit.unique_saturated_into()
}
}).saturating_add(T::DbWeight::get().reads(1)))]
#[pallet::weight({
let without_base_extrinsic_weight = false;
<T as pallet_evm::Config>::GasWeightMapping::gas_to_weight({
match xcm_transaction {
EthereumXcmTransaction::V1(v1_tx) => v1_tx.gas_limit.unique_saturated_into(),
EthereumXcmTransaction::V2(v2_tx) => v2_tx.gas_limit.unique_saturated_into()
}
}, without_base_extrinsic_weight).saturating_add(T::DbWeight::get().reads(1))
})]
pub fn transact(
origin: OriginFor<T>,
xcm_transaction: EthereumXcmTransaction,
Expand All @@ -162,12 +165,15 @@ pub mod pallet {

/// Xcm Transact an Ethereum transaction through proxy.
/// Weight: Gas limit plus the db reads involving the suspension and proxy checks
#[pallet::weight(<T as pallet_evm::Config>::GasWeightMapping::gas_to_weight({
match xcm_transaction {
EthereumXcmTransaction::V1(v1_tx) => v1_tx.gas_limit.unique_saturated_into(),
EthereumXcmTransaction::V2(v2_tx) => v2_tx.gas_limit.unique_saturated_into()
}
}).saturating_add(T::DbWeight::get().reads(2)))]
#[pallet::weight({
let without_base_extrinsic_weight = false;
<T as pallet_evm::Config>::GasWeightMapping::gas_to_weight({
match xcm_transaction {
EthereumXcmTransaction::V1(v1_tx) => v1_tx.gas_limit.unique_saturated_into(),
EthereumXcmTransaction::V2(v2_tx) => v2_tx.gas_limit.unique_saturated_into()
}
}, without_base_extrinsic_weight).saturating_add(T::DbWeight::get().reads(2))
})]
pub fn transact_through_proxy(
origin: OriginFor<T>,
transact_as: H160,
Expand Down
4 changes: 3 additions & 1 deletion pallets/ethereum-xcm/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ parameter_types! {
pub const ChainId: u64 = 42;
pub const EVMModuleId: PalletId = PalletId(*b"py/evmpa");
pub const BlockGasLimit: U256 = U256::MAX;
pub const WeightPerGas: u64 = 1;
}

pub struct HashedAddressMapping;
Expand All @@ -160,7 +161,8 @@ impl AddressMapping<AccountId32> for HashedAddressMapping {

impl pallet_evm::Config for Test {
type FeeCalculator = FixedGasPrice;
type GasWeightMapping = ();
type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
type WeightPerGas = WeightPerGas;
type CallOrigin = EnsureAddressTruncated;
type WithdrawOrigin = EnsureAddressTruncated;
type AddressMapping = HashedAddressMapping;
Expand Down
4 changes: 3 additions & 1 deletion precompiles/assets-erc20/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,13 @@ impl pallet_balances::Config for Runtime {
parameter_types! {
pub BlockGasLimit: U256 = U256::max_value();
pub const PrecompilesValue: Precompiles<Runtime> = Precompiles(PhantomData);
pub const WeightPerGas: u64 = 1;
}

impl pallet_evm::Config for Runtime {
type FeeCalculator = ();
type GasWeightMapping = ();
type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
type WeightPerGas = WeightPerGas;
type CallOrigin = EnsureAddressRoot<AccountId>;
type WithdrawOrigin = EnsureAddressNever<AccountId>;
type AddressMapping = AccountId;
Expand Down
4 changes: 3 additions & 1 deletion precompiles/author-mapping/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,13 @@ impl pallet_balances::Config for Runtime {
parameter_types! {
pub BlockGasLimit: U256 = U256::max_value();
pub const PrecompilesValue: Precompiles<Runtime> = Precompiles(PhantomData);
pub const WeightPerGas: u64 = 1;
}

impl pallet_evm::Config for Runtime {
type FeeCalculator = ();
type GasWeightMapping = ();
type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
type WeightPerGas = WeightPerGas;
type CallOrigin = EnsureAddressRoot<Account>;
type WithdrawOrigin = EnsureAddressNever<Account>;
type AddressMapping = Account;
Expand Down
4 changes: 3 additions & 1 deletion precompiles/balances-erc20/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,13 @@ impl pallet_balances::Config for Runtime {
parameter_types! {
pub BlockGasLimit: U256 = U256::max_value();
pub const PrecompilesValue: Precompiles<Runtime> = Precompiles(PhantomData);
pub const WeightPerGas: u64 = 1;
}

impl pallet_evm::Config for Runtime {
type FeeCalculator = ();
type GasWeightMapping = ();
type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
type WeightPerGas = WeightPerGas;
type CallOrigin = EnsureAddressRoot<AccountId>;
type WithdrawOrigin = EnsureAddressNever<AccountId>;
type AddressMapping = AccountId;
Expand Down
4 changes: 3 additions & 1 deletion precompiles/batch/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,13 @@ pub type PCall = BatchPrecompileCall<Runtime>;
parameter_types! {
pub BlockGasLimit: U256 = U256::max_value();
pub PrecompilesValue: TestPrecompiles<Runtime> = TestPrecompiles::new();
pub const WeightPerGas: u64 = 1;
}

impl pallet_evm::Config for Runtime {
type FeeCalculator = ();
type GasWeightMapping = ();
type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
type WeightPerGas = WeightPerGas;
type CallOrigin = EnsureAddressRoot<AccountId>;
type WithdrawOrigin = EnsureAddressNever<AccountId>;
type AddressMapping = AccountId;
Expand Down
4 changes: 3 additions & 1 deletion precompiles/call-permit/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,13 @@ pub type PCall = CallPermitPrecompileCall<Runtime>;

parameter_types! {
pub PrecompilesValue: TestPrecompiles<Runtime> = TestPrecompiles(Default::default());
pub const WeightPerGas: u64 = 1;
}

impl pallet_evm::Config for Runtime {
type FeeCalculator = ();
type GasWeightMapping = ();
type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
type WeightPerGas = WeightPerGas;
type CallOrigin = EnsureAddressRoot<AccountId>;
type WithdrawOrigin = EnsureAddressNever<AccountId>;
type AddressMapping = AccountId;
Expand Down
4 changes: 3 additions & 1 deletion precompiles/collective/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,13 @@ impl pallet_balances::Config for Runtime {
parameter_types! {
pub BlockGasLimit: U256 = U256::max_value();
pub const PrecompilesValue: Precompiles<Runtime> = Precompiles(PhantomData);
pub const WeightPerGas: u64 = 1;
}

impl pallet_evm::Config for Runtime {
type FeeCalculator = ();
type GasWeightMapping = ();
type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
type WeightPerGas = WeightPerGas;
type CallOrigin = EnsureAddressRoot<Account>;
type WithdrawOrigin = EnsureAddressNever<Account>;
type AddressMapping = Account;
Expand Down
4 changes: 3 additions & 1 deletion precompiles/crowdloan-rewards/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,13 @@ pub type PCall = CrowdloanRewardsPrecompileCall<Runtime>;
parameter_types! {
pub BlockGasLimit: U256 = U256::max_value();
pub const PrecompilesValue: TestPrecompiles<Runtime> = TestPrecompiles(PhantomData);
pub const WeightPerGas: u64 = 1;
}

impl pallet_evm::Config for Runtime {
type FeeCalculator = ();
type GasWeightMapping = ();
type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
type WeightPerGas = WeightPerGas;
type CallOrigin = EnsureAddressRoot<AccountId>;
type WithdrawOrigin = EnsureAddressNever<AccountId>;
type AddressMapping = IntoAddressMapping;
Expand Down
4 changes: 3 additions & 1 deletion precompiles/pallet-democracy/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,13 @@ impl pallet_balances::Config for Runtime {
parameter_types! {
pub BlockGasLimit: U256 = U256::max_value();
pub const PrecompilesValue: Precompiles<Runtime> = Precompiles(PhantomData);
pub const WeightPerGas: u64 = 1;
}

impl pallet_evm::Config for Runtime {
type FeeCalculator = ();
type GasWeightMapping = ();
type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
type WeightPerGas = WeightPerGas;
type CallOrigin = EnsureAddressRoot<Account>;
type WithdrawOrigin = EnsureAddressNever<Account>;
type AddressMapping = Account;
Expand Down
4 changes: 3 additions & 1 deletion precompiles/parachain-staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,13 @@ pub type PCall = ParachainStakingPrecompileCall<Runtime>;
parameter_types! {
pub BlockGasLimit: U256 = U256::max_value();
pub PrecompilesValue: TestPrecompiles<Runtime> = TestPrecompiles(Default::default());
pub const WeightPerGas: u64 = 1;
}

impl pallet_evm::Config for Runtime {
type FeeCalculator = ();
type GasWeightMapping = ();
type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
type WeightPerGas = WeightPerGas;
type CallOrigin = EnsureAddressRoot<AccountId>;
type WithdrawOrigin = EnsureAddressNever<AccountId>;
type AddressMapping = AccountId;
Expand Down
4 changes: 3 additions & 1 deletion precompiles/proxy/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,12 @@ impl<OuterOrigin> EnsureAddressOrigin<OuterOrigin> for EnsureAddressAlways {
parameter_types! {
pub BlockGasLimit: U256 = U256::max_value();
pub PrecompilesValue: TestPrecompiles<Runtime> = TestPrecompiles::new();
pub const WeightPerGas: u64 = 1;
}
impl pallet_evm::Config for Runtime {
type FeeCalculator = ();
type GasWeightMapping = ();
type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
type WeightPerGas = WeightPerGas;
type CallOrigin = EnsureAddressAlways;
type WithdrawOrigin = EnsureAddressNever<Account>;
type AddressMapping = Account;
Expand Down
4 changes: 3 additions & 1 deletion precompiles/randomness/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,13 @@ pub type TestPrecompiles<R> = PrecompileSetBuilder<

parameter_types! {
pub PrecompilesValue: TestPrecompiles<Runtime> = TestPrecompiles::new();
pub const WeightPerGas: u64 = 1;
}

impl pallet_evm::Config for Runtime {
type FeeCalculator = ();
type GasWeightMapping = ();
type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
type WeightPerGas = WeightPerGas;
type CallOrigin = EnsureAddressRoot<Account>;
type WithdrawOrigin = EnsureAddressNever<Account>;
type AddressMapping = IdentityAddressMapping;
Expand Down
4 changes: 3 additions & 1 deletion precompiles/relay-encoder/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,13 @@ pub type PCall = RelayEncoderPrecompileCall<Runtime, test_relay_runtime::TestEnc
parameter_types! {
pub BlockGasLimit: U256 = U256::max_value();
pub const PrecompilesValue: TestPrecompiles<Runtime> = TestPrecompiles(PhantomData);
pub const WeightPerGas: u64 = 1;
}

impl pallet_evm::Config for Runtime {
type FeeCalculator = ();
type GasWeightMapping = ();
type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
type WeightPerGas = WeightPerGas;
type CallOrigin = EnsureAddressRoot<Account>;
type WithdrawOrigin = EnsureAddressNever<Account>;
type AddressMapping = Account;
Expand Down
4 changes: 3 additions & 1 deletion precompiles/xcm-transactor/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,14 @@ pub type PCallV2 = XcmTransactorPrecompileV2Call<Runtime>;
parameter_types! {
pub BlockGasLimit: U256 = U256::max_value();
pub const PrecompilesValue: TestPrecompiles<Runtime> = TestPrecompiles(PhantomData);
pub const WeightPerGas: u64 = 1;
}

/// A mapping function that converts Ethereum gas to Substrate weight
/// We are mocking this 1-1 to test db read charges too
pub struct MockGasWeightMapping;
impl GasWeightMapping for MockGasWeightMapping {
fn gas_to_weight(gas: u64) -> Weight {
fn gas_to_weight(gas: u64, _without_base_weight: bool) -> Weight {
Weight::from_ref_time(gas)
}
fn weight_to_gas(weight: Weight) -> u64 {
Expand All @@ -280,6 +281,7 @@ impl GasWeightMapping for MockGasWeightMapping {
impl pallet_evm::Config for Runtime {
type FeeCalculator = ();
type GasWeightMapping = MockGasWeightMapping;
type WeightPerGas = WeightPerGas;
type CallOrigin = EnsureAddressRoot<TestAccount>;
type WithdrawOrigin = EnsureAddressNever<TestAccount>;
type AddressMapping = TestAccount;
Expand Down
4 changes: 3 additions & 1 deletion precompiles/xcm-utils/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,13 +307,14 @@ pub fn precompile_address() -> H160 {
parameter_types! {
pub BlockGasLimit: U256 = U256::max_value();
pub const PrecompilesValue: TestPrecompiles<Runtime> = TestPrecompiles(PhantomData);
pub const WeightPerGas: u64 = 1;
}

/// A mapping function that converts Ethereum gas to Substrate weight
/// We are mocking this 1-1 to test db read charges too
pub struct MockGasWeightMapping;
impl GasWeightMapping for MockGasWeightMapping {
fn gas_to_weight(gas: u64) -> Weight {
fn gas_to_weight(gas: u64, _without_base_weight: bool) -> Weight {
Weight::from_ref_time(gas)
}
fn weight_to_gas(weight: Weight) -> u64 {
Expand All @@ -324,6 +325,7 @@ impl GasWeightMapping for MockGasWeightMapping {
impl pallet_evm::Config for Runtime {
type FeeCalculator = ();
type GasWeightMapping = MockGasWeightMapping;
type WeightPerGas = WeightPerGas;
type CallOrigin = EnsureAddressRoot<TestAccount>;
type WithdrawOrigin = EnsureAddressNever<TestAccount>;
type AddressMapping = TestAccount;
Expand Down
4 changes: 3 additions & 1 deletion precompiles/xtokens/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,13 @@ pub fn precompile_address() -> H160 {
parameter_types! {
pub BlockGasLimit: U256 = U256::max_value();
pub const PrecompilesValue: TestPrecompiles<Runtime> = TestPrecompiles(PhantomData);
pub const WeightPerGas: u64 = 1;
}

impl pallet_evm::Config for Runtime {
type FeeCalculator = ();
type GasWeightMapping = ();
type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
type WeightPerGas = WeightPerGas;
type CallOrigin = EnsureAddressRoot<TestAccount>;
type WithdrawOrigin = EnsureAddressNever<TestAccount>;
type AddressMapping = TestAccount;
Expand Down
24 changes: 12 additions & 12 deletions runtime/moonbase/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,17 +376,6 @@ pub const GAS_PER_SECOND: u64 = 40_000_000;
/// u64 works for approximations because Weight is a very small unit compared to gas.
pub const WEIGHT_PER_GAS: u64 = WEIGHT_PER_SECOND.ref_time() / GAS_PER_SECOND;

pub struct MoonbeamGasWeightMapping;

impl pallet_evm::GasWeightMapping for MoonbeamGasWeightMapping {
fn gas_to_weight(gas: u64) -> Weight {
Weight::from_ref_time(gas.saturating_mul(WEIGHT_PER_GAS))
}
fn weight_to_gas(weight: Weight) -> u64 {
u64::try_from(weight.ref_time().wrapping_div(WEIGHT_PER_GAS)).unwrap_or(u32::MAX as u64)
}
}

parameter_types! {
pub BlockGasLimit: U256
= U256::from(NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT.ref_time() / WEIGHT_PER_GAS);
Expand All @@ -406,6 +395,7 @@ parameter_types! {
/// as a safety net.
pub MaximumMultiplier: Multiplier = Multiplier::from(100_000u128);
pub PrecompilesValue: MoonbasePrecompiles<Runtime> = MoonbasePrecompiles::<_>::new();
pub WeightPerGas: u64 = WEIGHT_PER_GAS;
}

pub struct FixedGasPrice;
Expand Down Expand Up @@ -458,7 +448,8 @@ moonbeam_runtime_common::impl_on_charge_evm_transaction!();

impl pallet_evm::Config for Runtime {
type FeeCalculator = FixedGasPrice;
type GasWeightMapping = MoonbeamGasWeightMapping;
type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
type WeightPerGas = WeightPerGas;
type BlockHashMapping = pallet_ethereum::EthereumBlockHashMapping<Self>;
type CallOrigin = EnsureAddressRoot<AccountId>;
type WithdrawOrigin = EnsureAddressNever<AccountId>;
Expand Down Expand Up @@ -1535,4 +1526,13 @@ mod tests {
);
}
}

#[test]
fn configured_base_extrinsic_weight_is_evm_compatible() {
let min_ethereum_transaction_weight = WeightPerGas::get() * 21_000;
let base_extrinsic = <Runtime as frame_system::Config>::BlockWeights::get()
.get(frame_support::weights::DispatchClass::Normal)
.base_extrinsic;
assert!(base_extrinsic.ref_time() <= min_ethereum_transaction_weight);
}
}
4 changes: 3 additions & 1 deletion runtime/moonbase/tests/xcm_mock/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -883,11 +883,13 @@ use sp_core::U256;

parameter_types! {
pub BlockGasLimit: U256 = U256::max_value();
pub WeightPerGas: u64 = 1;
}

impl pallet_evm::Config for Runtime {
type FeeCalculator = ();
type GasWeightMapping = ();
type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
type WeightPerGas = WeightPerGas;

type CallOrigin = pallet_evm::EnsureAddressRoot<AccountId>;
type WithdrawOrigin = pallet_evm::EnsureAddressNever<AccountId>;
Expand Down
Loading

0 comments on commit b083629

Please sign in to comment.