From 65cd94d7a8bf3f5abe615da0784321fa7110f982 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Thu, 28 Nov 2024 14:29:09 +0000 Subject: [PATCH] Decrease message severity for non-existing account (#7833) --- .../Processing/BlockProcessor.cs | 9 +----- .../Withdrawals/WithdrawalProcessor.cs | 9 +----- .../StateOverridesExtensions.cs | 2 +- .../Nethermind.Evm/VirtualMachine.cs | 29 ++----------------- .../Nethermind.State/IWorldState.cs | 2 +- .../Nethermind.State/StateProvider.cs | 6 ++-- src/Nethermind/Nethermind.State/WorldState.cs | 4 +-- 7 files changed, 13 insertions(+), 48 deletions(-) diff --git a/src/Nethermind/Nethermind.Consensus/Processing/BlockProcessor.cs b/src/Nethermind/Nethermind.Consensus/Processing/BlockProcessor.cs index 1bc42b7de83..154733f9903 100644 --- a/src/Nethermind/Nethermind.Consensus/Processing/BlockProcessor.cs +++ b/src/Nethermind/Nethermind.Consensus/Processing/BlockProcessor.cs @@ -435,14 +435,7 @@ private void ApplyMinerReward(Block block, BlockReward reward, IReleaseSpec spec { if (_logger.IsTrace) _logger.Trace($" {(BigInteger)reward.Value / (BigInteger)Unit.Ether:N3}{Unit.EthSymbol} for account at {reward.Address}"); - if (!_stateProvider.AccountExists(reward.Address)) - { - _stateProvider.CreateAccount(reward.Address, reward.Value); - } - else - { - _stateProvider.AddToBalance(reward.Address, reward.Value, spec); - } + _stateProvider.AddToBalanceAndCreateIfNotExists(reward.Address, reward.Value, spec); } // TODO: block processor pipeline diff --git a/src/Nethermind/Nethermind.Consensus/Withdrawals/WithdrawalProcessor.cs b/src/Nethermind/Nethermind.Consensus/Withdrawals/WithdrawalProcessor.cs index e315c5a5a8e..6ca5b013a04 100644 --- a/src/Nethermind/Nethermind.Consensus/Withdrawals/WithdrawalProcessor.cs +++ b/src/Nethermind/Nethermind.Consensus/Withdrawals/WithdrawalProcessor.cs @@ -38,14 +38,7 @@ public void ProcessWithdrawals(Block block, IReleaseSpec spec) if (_logger.IsTrace) _logger.Trace($" {withdrawal.AmountInGwei} GWei to account {withdrawal.Address}"); // Consensus clients are using Gwei for withdrawals amount. We need to convert it to Wei before applying state changes https://github.com/ethereum/execution-apis/pull/354 - if (_stateProvider.AccountExists(withdrawal.Address)) - { - _stateProvider.AddToBalance(withdrawal.Address, withdrawal.AmountInWei, spec); - } - else - { - _stateProvider.CreateAccount(withdrawal.Address, withdrawal.AmountInWei); - } + _stateProvider.AddToBalanceAndCreateIfNotExists(withdrawal.Address, withdrawal.AmountInWei, spec); } } diff --git a/src/Nethermind/Nethermind.Evm/StateOverridesExtensions.cs b/src/Nethermind/Nethermind.Evm/StateOverridesExtensions.cs index d34ef57f238..95fea8ff3eb 100644 --- a/src/Nethermind/Nethermind.Evm/StateOverridesExtensions.cs +++ b/src/Nethermind/Nethermind.Evm/StateOverridesExtensions.cs @@ -124,7 +124,7 @@ private static void UpdateBalance( } else if (balance < newBalance) { - stateProvider.AddToBalance(address, newBalance - balance, spec); + stateProvider.AddToBalanceAndCreateIfNotExists(address, newBalance - balance, spec); } } } diff --git a/src/Nethermind/Nethermind.Evm/VirtualMachine.cs b/src/Nethermind/Nethermind.Evm/VirtualMachine.cs index 4cd81e5d32c..3512f53574c 100644 --- a/src/Nethermind/Nethermind.Evm/VirtualMachine.cs +++ b/src/Nethermind/Nethermind.Evm/VirtualMachine.cs @@ -574,16 +574,7 @@ private CallResult ExecutePrecompile(EvmState state, IReleaseSpec spec) long baseGasCost = precompile.BaseGasCost(spec); long blobGasCost = precompile.DataGasCost(callData, spec); - bool wasCreated = false; - if (!_state.AccountExists(state.Env.ExecutingAccount)) - { - wasCreated = true; - _state.CreateAccount(state.Env.ExecutingAccount, transferValue); - } - else - { - _state.AddToBalance(state.Env.ExecutingAccount, transferValue, spec); - } + bool wasCreated = _state.AddToBalanceAndCreateIfNotExists(state.Env.ExecutingAccount, transferValue, spec); // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-161.md // An additional issue was found in Parity, @@ -639,14 +630,7 @@ private CallResult ExecuteCall(EvmState vmState, ReadOnlyM ref readonly ExecutionEnvironment env = ref vmState.Env; if (!vmState.IsContinuation) { - if (!_state.AccountExists(env.ExecutingAccount)) - { - _state.CreateAccount(env.ExecutingAccount, env.TransferValue); - } - else - { - _state.AddToBalance(env.ExecutingAccount, env.TransferValue, spec); - } + _state.AddToBalanceAndCreateIfNotExists(env.ExecutingAccount, env.TransferValue, spec); if (vmState.ExecutionType.IsAnyCreate() && spec.ClearEmptyAccountWhenTouched) { @@ -2247,14 +2231,7 @@ private EvmExceptionType InstructionCall( EvmExceptionType FastCall(IReleaseSpec spec, out object returnData, in UInt256 transferValue, Address target) { - if (!_state.AccountExists(target)) - { - _state.CreateAccount(target, transferValue); - } - else - { - _state.AddToBalance(target, transferValue, spec); - } + _state.AddToBalanceAndCreateIfNotExists(target, transferValue, spec); Metrics.IncrementEmptyCalls(); returnData = CallResult.BoxedEmpty; diff --git a/src/Nethermind/Nethermind.State/IWorldState.cs b/src/Nethermind/Nethermind.State/IWorldState.cs index bc097af8ddd..65d2216a0f8 100644 --- a/src/Nethermind/Nethermind.State/IWorldState.cs +++ b/src/Nethermind/Nethermind.State/IWorldState.cs @@ -90,7 +90,7 @@ public interface IWorldState : IJournal, IReadOnlyStateProvider void AddToBalance(Address address, in UInt256 balanceChange, IReleaseSpec spec); - void AddToBalanceAndCreateIfNotExists(Address address, in UInt256 balanceChange, IReleaseSpec spec); + bool AddToBalanceAndCreateIfNotExists(Address address, in UInt256 balanceChange, IReleaseSpec spec); void SubtractFromBalance(Address address, in UInt256 balanceChange, IReleaseSpec spec); diff --git a/src/Nethermind/Nethermind.State/StateProvider.cs b/src/Nethermind/Nethermind.State/StateProvider.cs index 02d9d1944b2..0e10c6e4132 100644 --- a/src/Nethermind/Nethermind.State/StateProvider.cs +++ b/src/Nethermind/Nethermind.State/StateProvider.cs @@ -177,7 +177,7 @@ Account GetThroughCacheCheckExists() Account result = GetThroughCache(address); if (result is null) { - if (_logger.IsError) _logger.Error("Updating balance of a non-existing account"); + if (_logger.IsDebug) _logger.Debug("Updating balance of a non-existing account"); throw new InvalidOperationException("Updating balance of a non-existing account"); } @@ -383,15 +383,17 @@ public void CreateAccountIfNotExists(Address address, in UInt256 balance, in UIn } } - public void AddToBalanceAndCreateIfNotExists(Address address, in UInt256 balance, IReleaseSpec spec) + public bool AddToBalanceAndCreateIfNotExists(Address address, in UInt256 balance, IReleaseSpec spec) { if (AccountExists(address)) { AddToBalance(address, balance, spec); + return false; } else { CreateAccount(address, balance); + return true; } } diff --git a/src/Nethermind/Nethermind.State/WorldState.cs b/src/Nethermind/Nethermind.State/WorldState.cs index 51f819f6369..19ad775d2fb 100644 --- a/src/Nethermind/Nethermind.State/WorldState.cs +++ b/src/Nethermind/Nethermind.State/WorldState.cs @@ -151,9 +151,9 @@ public void AddToBalance(Address address, in UInt256 balanceChange, IReleaseSpec { _stateProvider.AddToBalance(address, balanceChange, spec); } - public void AddToBalanceAndCreateIfNotExists(Address address, in UInt256 balanceChange, IReleaseSpec spec) + public bool AddToBalanceAndCreateIfNotExists(Address address, in UInt256 balanceChange, IReleaseSpec spec) { - _stateProvider.AddToBalanceAndCreateIfNotExists(address, balanceChange, spec); + return _stateProvider.AddToBalanceAndCreateIfNotExists(address, balanceChange, spec); } public void SubtractFromBalance(Address address, in UInt256 balanceChange, IReleaseSpec spec) {