Skip to content

Commit 87706e9

Browse files
LukaszRozmejkamilchodola
authored andcommitted
Fix by setting the cache to empty items (#7197)
1 parent 372ec53 commit 87706e9

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/Nethermind/Nethermind.State.Test/StorageProviderTests.cs

+20-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Nethermind.Core.Crypto;
88
using Nethermind.Core.Extensions;
99
using Nethermind.Core.Resettables;
10+
using Nethermind.Core.Test.Builders;
1011
using Nethermind.Db;
1112
using Nethermind.Specs.Forks;
1213
using Nethermind.Logging;
@@ -409,16 +410,33 @@ public void Persistent_state_restores_independent_of_transient_state(int snapsho
409410
_values[snapshot + 1].Should().BeEquivalentTo(provider.Get(new StorageCell(ctx.Address1, 1)).ToArray());
410411
}
411412

413+
/// <summary>
414+
/// Reset will reset transient state
415+
/// </summary>
416+
[Test]
417+
public void Selfdestruct_clears_cache()
418+
{
419+
PreBlockCaches preBlockCaches = new PreBlockCaches();
420+
Context ctx = new(preBlockCaches);
421+
WorldState provider = BuildStorageProvider(ctx);
422+
StorageCell storageCell = new StorageCell(TestItem.AddressA, 1);
423+
preBlockCaches.StorageCache[storageCell] = [1, 2, 3];
424+
provider.Get(storageCell);
425+
provider.Commit(Paris.Instance);
426+
provider.ClearStorage(TestItem.AddressA);
427+
provider.Get(storageCell).ToArray().Should().BeEquivalentTo(StorageTree.EmptyBytes);
428+
}
429+
412430
private class Context
413431
{
414432
public WorldState StateProvider { get; }
415433

416434
public readonly Address Address1 = new(Keccak.Compute("1"));
417435
public readonly Address Address2 = new(Keccak.Compute("2"));
418436

419-
public Context()
437+
public Context(PreBlockCaches preBlockCaches = null)
420438
{
421-
StateProvider = new WorldState(new TrieStore(new MemDb(), LimboLogs.Instance), Substitute.For<IDb>(), LogManager);
439+
StateProvider = new WorldState(new TrieStore(new MemDb(), LimboLogs.Instance), Substitute.For<IDb>(), LogManager, preBlockCaches);
422440
StateProvider.CreateAccount(Address1, 0);
423441
StateProvider.CreateAccount(Address2, 0);
424442
StateProvider.Commit(Frontier.Instance);

src/Nethermind/Nethermind.State/PersistentStorageProvider.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,13 @@ public override void ClearStorage(Address address)
428428
base.ClearStorage(address);
429429

430430
// Bit heavy-handed, but we need to clear all the cache for that address
431-
_blockCache.Remove(address);
431+
if (_blockCache.TryGetValue(address, out Dictionary<UInt256, byte[]> values))
432+
{
433+
foreach (UInt256 storageSlot in values.Keys)
434+
{
435+
values[storageSlot] = StorageTree.EmptyBytes;
436+
}
437+
}
432438

433439
// here it is important to make sure that we will not reuse the same tree when the contract is revived
434440
// by means of CREATE 2 - notice that the cached trie may carry information about items that were not

0 commit comments

Comments
 (0)