Skip to content

Commit

Permalink
Align GasWeightMapping with Substrate do_pre_dispatch logic (#1884)
Browse files Browse the repository at this point in the history
* 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 Oct 20, 2022
1 parent 1381564 commit 5b9078e
Show file tree
Hide file tree
Showing 28 changed files with 138 additions and 95 deletions.
40 changes: 20 additions & 20 deletions Cargo.lock

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

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
Loading

0 comments on commit 5b9078e

Please sign in to comment.