diff --git a/frame/ethereum/src/lib.rs b/frame/ethereum/src/lib.rs index 4e949e0f5a..136a8f06ed 100644 --- a/frame/ethereum/src/lib.rs +++ b/frame/ethereum/src/lib.rs @@ -814,7 +814,7 @@ impl Pallet { target, input, value, - gas_limit.low_u64(), + gas_limit.unique_saturated_into(), max_fee_per_gas, max_priority_fee_per_gas, nonce, @@ -841,7 +841,7 @@ impl Pallet { from, input, value, - gas_limit.low_u64(), + gas_limit.unique_saturated_into(), max_fee_per_gas, max_priority_fee_per_gas, nonce, diff --git a/frame/evm/src/lib.rs b/frame/evm/src/lib.rs index 1ead3cda36..6b9028d570 100644 --- a/frame/evm/src/lib.rs +++ b/frame/evm/src/lib.rs @@ -77,7 +77,7 @@ use sp_runtime::{ traits::{BadOrigin, Saturating, UniqueSaturatedInto, Zero}, AccountId32, DispatchErrorWithPostInfo, }; -use sp_std::vec::Vec; +use sp_std::{cmp::min, vec::Vec}; pub use evm::{ Config as EvmConfig, Context, ExitError, ExitFatal, ExitReason, ExitRevert, ExitSucceed, @@ -410,21 +410,26 @@ pub mod pallet { } #[pallet::genesis_build] - impl GenesisBuild for GenesisConfig { + impl GenesisBuild for GenesisConfig + where + U256: UniqueSaturatedInto>, + { fn build(&self) { + const MAX_ACCOUNT_NONCE: usize = 100; + for (address, account) in &self.accounts { let account_id = T::AddressMapping::into_account_id(*address); // ASSUME: in one single EVM transaction, the nonce will not increase more than // `u128::max_value()`. - for _ in 0..account.nonce.low_u128() { + for _ in 0..min( + MAX_ACCOUNT_NONCE, + UniqueSaturatedInto::::unique_saturated_into(account.nonce), + ) { frame_system::Pallet::::inc_account_nonce(&account_id); } - T::Currency::deposit_creating( - &account_id, - account.balance.low_u128().unique_saturated_into(), - ); + T::Currency::deposit_creating(&account_id, account.balance.unique_saturated_into()); Pallet::::create_account(*address, account.code.clone()); @@ -707,6 +712,7 @@ where Opposite = C::PositiveImbalance, >, OU: OnUnbalanced>, + U256: UniqueSaturatedInto<::AccountId>>::Balance>, { // Kept type as Option to satisfy bound of Default type LiquidityInfo = Option>; @@ -718,7 +724,7 @@ where let account_id = T::AddressMapping::into_account_id(*who); let imbalance = C::withdraw( &account_id, - fee.low_u128().unique_saturated_into(), + fee.unique_saturated_into(), WithdrawReasons::FEE, ExistenceRequirement::AllowDeath, ) @@ -738,7 +744,7 @@ where // Calculate how much refund we should return let refund_amount = paid .peek() - .saturating_sub(corrected_fee.low_u128().unique_saturated_into()); + .saturating_sub(corrected_fee.unique_saturated_into()); // refund to the account that paid the fees. If this fails, the // account might have dropped below the existential balance. In // that case we don't refund anything. @@ -769,7 +775,7 @@ where .same() .unwrap_or_else(|_| C::NegativeImbalance::zero()); - let (base_fee, tip) = adjusted_paid.split(base_fee.low_u128().unique_saturated_into()); + let (base_fee, tip) = adjusted_paid.split(base_fee.unique_saturated_into()); // Handle base fee. Can be either burned, rationed, etc ... OU::on_unbalanced(base_fee); return Some(tip); @@ -793,7 +799,10 @@ impl OnChargeEVMTransaction for () ::AccountId>>::PositiveImbalance: Imbalance<::AccountId>>::Balance, Opposite = ::AccountId>>::NegativeImbalance>, ::AccountId>>::NegativeImbalance: - Imbalance<::AccountId>>::Balance, Opposite = ::AccountId>>::PositiveImbalance>, { +Imbalance<::AccountId>>::Balance, Opposite = ::AccountId>>::PositiveImbalance>, +U256: UniqueSaturatedInto>, + +{ // Kept type as Option to satisfy bound of Default type LiquidityInfo = Option>; diff --git a/frame/evm/src/runner/stack.rs b/frame/evm/src/runner/stack.rs index 8cbfdb4159..c1402e9c5a 100644 --- a/frame/evm/src/runner/stack.rs +++ b/frame/evm/src/runner/stack.rs @@ -18,8 +18,9 @@ //! EVM stack-based runner. use crate::{ - runner::Runner as RunnerT, AccountCodes, AccountStorages, AddressMapping, BlockHashMapping, - Config, Error, Event, FeeCalculator, OnChargeEVMTransaction, Pallet, RunnerError, + runner::Runner as RunnerT, AccountCodes, AccountStorages, AddressMapping, BalanceOf, + BlockHashMapping, Config, Error, Event, FeeCalculator, OnChargeEVMTransaction, Pallet, + RunnerError, }; use evm::{ backend::Backend as BackendT, @@ -41,8 +42,11 @@ pub struct Runner { _marker: PhantomData, } -impl Runner { - /// Execute an EVM operation. +impl Runner +where + BalanceOf: TryFrom + Into, +{ + /// Execute an already validated EVM operation. pub fn execute<'config, 'precompiles, F, R>( source: H160, value: U256, @@ -245,7 +249,10 @@ impl Runner { } } -impl RunnerT for Runner { +impl RunnerT for Runner +where + BalanceOf: TryFrom + Into, +{ type Error = Error; fn call( @@ -542,6 +549,8 @@ impl<'vicinity, 'config, T: Config> BackendT for SubstrateStackState<'vicinity, impl<'vicinity, 'config, T: Config> StackStateT<'config> for SubstrateStackState<'vicinity, 'config, T> +where + BalanceOf: TryFrom + Into, { fn metadata(&self) -> &StackSubstateMetadata<'config> { self.substate.metadata() @@ -630,7 +639,10 @@ impl<'vicinity, 'config, T: Config> StackStateT<'config> T::Currency::transfer( &source, &target, - transfer.value.low_u128().unique_saturated_into(), + transfer + .value + .try_into() + .map_err(|_| ExitError::OutOfFund)?, ExistenceRequirement::AllowDeath, ) .map_err(|_| ExitError::OutOfFund) diff --git a/frame/evm/src/tests.rs b/frame/evm/src/tests.rs index b1f23882aa..c7198bf86e 100644 --- a/frame/evm/src/tests.rs +++ b/frame/evm/src/tests.rs @@ -268,8 +268,10 @@ fn issuance_after_tip() { result.expect("EVM can be called"); let after_tip = ::Currency::total_issuance(); // Only base fee is burned - let (base_fee, _) = ::FeeCalculator::min_gas_price(); - assert_eq!(after_tip, (before_tip - (base_fee.low_u64() * 21_000))); + let base_fee: u64 = ::FeeCalculator::min_gas_price() + .0 + .unique_saturated_into(); + assert_eq!(after_tip, (before_tip - (base_fee * 21_000))); }); } @@ -355,7 +357,7 @@ fn refunds_and_priority_should_work() { assert_eq!(after_call, before_call - total_cost); let after_tip = EVM::account_basic(&author).0.balance; - assert_eq!(after_tip, (before_tip + actual_tip.low_u128())); + assert_eq!(after_tip, (before_tip + actual_tip)); }); } diff --git a/template/runtime/src/lib.rs b/template/runtime/src/lib.rs index ed9b991e80..eb1f08d176 100644 --- a/template/runtime/src/lib.rs +++ b/template/runtime/src/lib.rs @@ -24,7 +24,7 @@ use sp_runtime::{ create_runtime_str, generic, impl_opaque_keys, traits::{ AccountIdLookup, BlakeTwo256, Block as BlockT, DispatchInfoOf, Dispatchable, - IdentifyAccount, NumberFor, PostDispatchInfoOf, Verify, + IdentifyAccount, NumberFor, PostDispatchInfoOf, UniqueSaturatedInto, Verify, }, transaction_validity::{TransactionSource, TransactionValidity, TransactionValidityError}, ApplyExtrinsicResult, MultiSignature, @@ -663,7 +663,7 @@ impl_runtime_apis! { to, data, value, - gas_limit.low_u64(), + gas_limit.unique_saturated_into(), max_fee_per_gas, max_priority_fee_per_gas, nonce, @@ -697,7 +697,7 @@ impl_runtime_apis! { from, data, value, - gas_limit.low_u64(), + gas_limit.unique_saturated_into(), max_fee_per_gas, max_priority_fee_per_gas, nonce,