From 3bcf9bfaa24e0480f5a9db46213b0c58aa574567 Mon Sep 17 00:00:00 2001 From: gd-0 <90608901+gd-0@users.noreply.github.com> Date: Tue, 7 Feb 2023 19:27:46 +0000 Subject: [PATCH 1/4] use gas price in place of effective gas price for initial balance check --- crates/revm/src/evm_impl.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/revm/src/evm_impl.rs b/crates/revm/src/evm_impl.rs index b4b38a5ce8..313544cdd0 100644 --- a/crates/revm/src/evm_impl.rs +++ b/crates/revm/src/evm_impl.rs @@ -112,7 +112,7 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> Transact .balance; let payment_value = U256::from(gas_limit) - .checked_mul(effective_gas_price) + .checked_mul(self.data.env.tx.gas_price) .ok_or(EVMError::Transaction( InvalidTransaction::OverflowPaymentInTransaction, ))?; From 8f623bfdad2ab317aaa43b83c32369e95dd560e7 Mon Sep 17 00:00:00 2001 From: gd-0 <90608901+gd-0@users.noreply.github.com> Date: Wed, 8 Feb 2023 13:05:02 +0000 Subject: [PATCH 2/4] use gas price when refunding account --- crates/revm/src/evm_impl.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/revm/src/evm_impl.rs b/crates/revm/src/evm_impl.rs index 313544cdd0..2ffa354394 100644 --- a/crates/revm/src/evm_impl.rs +++ b/crates/revm/src/evm_impl.rs @@ -282,7 +282,7 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> EVMImpl<'a, GSPEC, DB, acc_caller.info.balance = acc_caller .info .balance - .saturating_add(effective_gas_price * U256::from(gas.remaining() + gas_refunded)); + .saturating_add(self.data.env.tx.gas_price * U256::from(gas.remaining() + gas_refunded)); // EIP-1559 let coinbase_gas_price = if SPEC::enabled(LONDON) { From cbbcb5cafc3c3fc7b07b999116ea9646f8bb3ffb Mon Sep 17 00:00:00 2001 From: gd-0 <90608901+gd-0@users.noreply.github.com> Date: Wed, 8 Feb 2023 16:35:14 +0000 Subject: [PATCH 3/4] sub effective_price * gas from caller balance instead of max_fee --- crates/revm/src/evm_impl.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/revm/src/evm_impl.rs b/crates/revm/src/evm_impl.rs index 2ffa354394..292a4cdcf2 100644 --- a/crates/revm/src/evm_impl.rs +++ b/crates/revm/src/evm_impl.rs @@ -111,7 +111,7 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> Transact .info .balance; - let payment_value = U256::from(gas_limit) + let balance_check = U256::from(gas_limit) .checked_mul(self.data.env.tx.gas_price) .ok_or(EVMError::Transaction( InvalidTransaction::OverflowPaymentInTransaction, @@ -119,14 +119,14 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> Transact // Check if account has enough balance for gas_limit*gas_price and value transfer. // Transfer will be done inside `*_inner` functions. - if payment_value + value > *caller_balance && !disable_balance_check { + if balance_check + value > *caller_balance && !disable_balance_check { return Err(InvalidTransaction::LackOfFundForGasLimit.into()); } // Reduce gas_limit*gas_price amount of caller account. // unwrap_or can only occur if disable_balance_check is enabled *caller_balance = caller_balance - .checked_sub(payment_value) + .checked_sub(U256::from(gas_limit) * effective_gas_price) .unwrap_or(U256::ZERO); let mut gas = Gas::new(gas_limit); @@ -282,7 +282,7 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> EVMImpl<'a, GSPEC, DB, acc_caller.info.balance = acc_caller .info .balance - .saturating_add(self.data.env.tx.gas_price * U256::from(gas.remaining() + gas_refunded)); + .saturating_add(effective_gas_price * U256::from(gas.remaining() + gas_refunded)); // EIP-1559 let coinbase_gas_price = if SPEC::enabled(LONDON) { From 2c4cb1f85626e1ab0917f81024c24a97cd700e1d Mon Sep 17 00:00:00 2001 From: gd-0 <90608901+gd-0@users.noreply.github.com> Date: Thu, 9 Feb 2023 11:02:29 +0000 Subject: [PATCH 4/4] checked arithmetic for balance_check + value --- crates/revm/src/evm_impl.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/revm/src/evm_impl.rs b/crates/revm/src/evm_impl.rs index 292a4cdcf2..eb7041c382 100644 --- a/crates/revm/src/evm_impl.rs +++ b/crates/revm/src/evm_impl.rs @@ -113,13 +113,14 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> Transact let balance_check = U256::from(gas_limit) .checked_mul(self.data.env.tx.gas_price) + .and_then(|gas_cost| gas_cost.checked_add(value)) .ok_or(EVMError::Transaction( InvalidTransaction::OverflowPaymentInTransaction, ))?; // Check if account has enough balance for gas_limit*gas_price and value transfer. // Transfer will be done inside `*_inner` functions. - if balance_check + value > *caller_balance && !disable_balance_check { + if balance_check > *caller_balance && !disable_balance_check { return Err(InvalidTransaction::LackOfFundForGasLimit.into()); }