From f42dbeb4013f239c07f99add4bd14b5cbe79eb58 Mon Sep 17 00:00:00 2001 From: icodezjb Date: Sun, 4 Feb 2024 18:51:48 +0800 Subject: [PATCH] Check balance overflow --- frame/evm/src/runner/stack.rs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/frame/evm/src/runner/stack.rs b/frame/evm/src/runner/stack.rs index 828d5d8fcd..09ff9759f3 100644 --- a/frame/evm/src/runner/stack.rs +++ b/frame/evm/src/runner/stack.rs @@ -19,7 +19,7 @@ use crate::{ chainx_value_shrink, runner::Runner as RunnerT, AccountCodes, AccountStorages, AddressMapping, - BlockHashMapping, Config, Error, Event, FeeCalculator, OnChargeEVMTransaction, Pallet, + BlockHashMapping, Config, Error, Event, FeeCalculator, OnChargeEVMTransaction, Pallet, BalanceOf }; use evm::{ backend::Backend as BackendT, @@ -41,7 +41,10 @@ pub struct Runner { _marker: PhantomData, } -impl Runner { +impl Runner +where + BalanceOf: TryFrom + Into, +{ /// Execute an EVM operation. pub fn execute<'config, 'precompiles, F, R>( source: H160, @@ -223,7 +226,10 @@ impl Runner { } } -impl RunnerT for Runner { +impl RunnerT for Runner +where + BalanceOf: TryFrom + Into, +{ type Error = Error; fn call( @@ -521,6 +527,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() @@ -611,10 +619,12 @@ impl<'vicinity, 'config, T: Config> StackStateT<'config> T::Currency::transfer( &source, &target, - value.low_u128().unique_saturated_into(), + value + .try_into() + .map_err(|_| ExitError::OutOfFund)?, ExistenceRequirement::AllowDeath, ) - .map_err(|_| ExitError::OutOfFund) + .map_err(|_| ExitError::OutOfFund) } fn reset_balance(&mut self, _address: H160) {