Skip to content

Commit 1b0746e

Browse files
committed
Fix self destruct missing clear
1 parent b5731c0 commit 1b0746e

File tree

6 files changed

+40
-17
lines changed

6 files changed

+40
-17
lines changed

src/Nethermind/Nethermind.Db.Rocks/Config/DbConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,5 +508,5 @@ public class DbConfig : IDbConfig
508508
TrieNodeConfig +
509509
"";
510510
public string? FlatStorageTopNodesNodesDbAdditionalRocksDbOptions { get; set; }
511-
public ulong PreimageDbWriteBufferSize { get; set; } = (ulong)256.MiB();
511+
public ulong PreimageDbWriteBufferSize { get; set; } = (ulong)16.MiB();
512512
}

src/Nethermind/Nethermind.Evm/TransactionProcessing/TransactionProcessor.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,7 @@ private TransactionResult BuildExecutionEnvironment(
629629

630630
protected virtual bool ShouldValidate(ExecutionOptions opts) => !opts.HasFlag(ExecutionOptions.SkipValidation);
631631

632+
[MethodImpl(MethodImplOptions.NoOptimization)]
632633
private int ExecuteEvmCall<TTracingInst>(
633634
Transaction tx,
634635
BlockHeader header,

src/Nethermind/Nethermind.State/Flat/FlatDiffRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ public void Add(Snapshot snapshot)
691691
}
692692
if (deleted > 0)
693693
{
694-
_logger.Warn($"Should selfdestruct {toSelfDestructStorage.Key}. Deleted {deleted}");
694+
_logger.Warn($"Should selfdestruct {toSelfDestructStorage.Key}. Deleted {deleted}. Snapshot range {snapshot.From} {snapshot.To}");
695695
throw new Exception($"Should sefl destruct not called properly {toSelfDestructStorage.Key}");
696696
}
697697
continue;

src/Nethermind/Nethermind.State/Flat/SnapshotBundle.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ public void SetAccount(AddressAsKey addr, Account? account)
464464
{
465465
if (addr == FlatWorldStateScope.DebugAddress)
466466
{
467-
Console.Error.WriteLine("set to {a}");
467+
Console.Error.WriteLine($"set to {account}");
468468
}
469469
_changedAccounts[addr] = account;
470470
}
@@ -549,13 +549,17 @@ public Snapshot CompactToKnownState()
549549
{
550550
var address = addrK.Key;
551551
var isNewAccount = addrK.Value;
552-
selfDestructedStorageAddresses[address] = isNewAccount;
553-
554552
if (!isNewAccount)
555553
{
554+
selfDestructedStorageAddresses[address] = false;
556555
addressToClear.Add(address);
557556
addressHashToClear.Add(address.Value.ToAccountPath.ToCommitment());
558557
}
558+
else
559+
{
560+
// Note, if its already false, we should not set it to true
561+
selfDestructedStorageAddresses.TryAdd(address, true);
562+
}
559563
}
560564

561565
if (addressToClear.Count > 0)
@@ -638,7 +642,7 @@ public void Clear(Address address, Hash256AsKey addressHash)
638642
isNewAccount = account == null;
639643
if (address == FlatWorldStateScope.DebugAddress)
640644
{
641-
Console.Error.WriteLine("The clear is is new");
645+
Console.Error.WriteLine($"The clear newness is {isNewAccount}");
642646
}
643647
}
644648
else

src/Nethermind/Nethermind.State/PersistentStorageProvider.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using Nethermind.Evm.Tracing.State;
1919
using Nethermind.Int256;
2020
using Nethermind.Logging;
21+
using Nethermind.State.Flat.ScopeProvider;
2122
using Nethermind.Trie;
2223

2324
namespace Nethermind.State;
@@ -214,7 +215,9 @@ protected override void CommitCore(IStorageTracer tracer)
214215
foreach (AddressAsKey address in toUpdateRoots)
215216
{
216217
// since the accounts could be empty accounts that are removing (EIP-158)
217-
if (_stateProvider.AccountExists(address))
218+
// Note: it could be that the TX remove the account but the underlying account was originally not missing
219+
// hence, the storage Clear need to be called, hence the additional call to _currentScope.Get
220+
if (_stateProvider.AccountExists(address) || _currentScope.Get(address) is not null)
218221
{
219222
_toUpdateRoots[address] = true;
220223
// Add storage tree, will accessed later, which may be in parallel
@@ -268,6 +271,7 @@ void UpdateRootHashesSingleThread()
268271
{
269272
if (!_toUpdateRoots.TryGetValue(kvp.Key, out bool hasChanges) || !hasChanges)
270273
{
274+
if (kvp.Key == FlatWorldStateScope.DebugAddress) Console.Error.WriteLine("Update skipped");
271275
// Wasn't updated don't recalculate
272276
continue;
273277
}
@@ -402,6 +406,11 @@ public override void ClearStorage(Address address)
402406
{
403407
base.ClearStorage(address);
404408

409+
if (address == FlatWorldStateScope.DebugAddress)
410+
{
411+
Console.Error.WriteLine($"Clear on world state for debug address {address}");
412+
}
413+
405414
_toUpdateRoots.TryAdd(address, true);
406415

407416
PerContractState state = GetOrCreateStorage(address);
@@ -561,6 +570,11 @@ private byte[] LoadFromTreeStorage(StorageCell storageCell)
561570
int writes = 0;
562571
int skipped = 0;
563572

573+
if (_address == FlatWorldStateScope.DebugAddress && BlockChange.HasClear)
574+
{
575+
Console.Error.WriteLine("Clear in process storage change");
576+
}
577+
564578
if (BlockChange.HasClear)
565579
{
566580
storageWriteBatch.Clear();

src/Nethermind/Nethermind.State/StateProvider.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using Nethermind.Evm.Tracing.State;
2323
using Nethermind.Int256;
2424
using Nethermind.Logging;
25+
using Nethermind.State.Flat.ScopeProvider;
2526
using Metrics = Nethermind.Db.Metrics;
2627
using static Nethermind.State.StateProvider;
2728

@@ -177,14 +178,11 @@ static Account ThrowIfNull(Address address)
177178
=> throw new InvalidOperationException($"Account {address} is null when updating code hash");
178179
}
179180

180-
// 0x9290084cde3cfc1085e5e8aa6ff1fb141b3cebc4ac64f4ce937a2c9b5cd85a69, // Hash
181-
private Address importantAddress = new Address("0xf664829682daf488be5318dae198a69ce27fb31b");
182-
183181
private void SetNewBalance(Address address, in UInt256 balanceChange, IReleaseSpec releaseSpec, bool isSubtracting)
184182
{
185183
_needsStateRootUpdate = true;
186184

187-
if (address == importantAddress)
185+
if (address == FlatWorldStateScope.DebugAddress)
188186
{
189187
Console.Error.WriteLine($"Balance change {balanceChange}, {isSubtracting}");
190188
}
@@ -335,9 +333,9 @@ public byte[] GetCode(Address address)
335333

336334
public void DeleteAccount(Address address)
337335
{
338-
if (address == importantAddress)
336+
if (address == FlatWorldStateScope.DebugAddress)
339337
{
340-
Console.Error.WriteLine($"delete address");
338+
Console.Error.WriteLine($"delete address {Environment.StackTrace}");
341339
}
342340
_needsStateRootUpdate = true;
343341
PushDelete(address);
@@ -787,7 +785,13 @@ private void PushTouch(Address address, Account account, IReleaseSpec releaseSpe
787785
}
788786

789787
private void PushDelete(Address address)
790-
=> Push(address, null, ChangeType.Delete);
788+
{
789+
Push(address, null, ChangeType.Delete);
790+
if (address == FlatWorldStateScope.DebugAddress)
791+
{
792+
Console.Error.WriteLine("Delete account");
793+
}
794+
}
791795

792796
private void Push(Address address, Account? touchedAccount, ChangeType changeType)
793797
{
@@ -798,7 +802,7 @@ private void Push(Address address, Account? touchedAccount, ChangeType changeTyp
798802
return;
799803
}
800804

801-
if (address == importantAddress)
805+
if (address == FlatWorldStateScope.DebugAddress)
802806
{
803807
Console.Error.WriteLine($"Push {changeType}, {touchedAccount}");
804808
}
@@ -811,7 +815,7 @@ private void PushNew(Address address, Account account)
811815
Stack<int> stack = SetupCache(address);
812816
stack.Push(_changes.Count);
813817
_changes.Add(new Change(address, account, ChangeType.New));
814-
if (address == importantAddress)
818+
if (address == FlatWorldStateScope.DebugAddress)
815819
{
816820
Console.Error.WriteLine($"Push new {account}");
817821
}
@@ -822,7 +826,7 @@ private void PushRecreateEmpty(Address address, Account account, Stack<int> stac
822826
{
823827
stack.Push(_changes.Count);
824828
_changes.Add(new Change(address, account, ChangeType.RecreateEmpty));
825-
if (address == importantAddress)
829+
if (address == FlatWorldStateScope.DebugAddress)
826830
{
827831
Console.Error.WriteLine($"Push recreate empty {account}");
828832
}

0 commit comments

Comments
 (0)