Skip to content

Commit

Permalink
Fix by setting the cache to empty items (#7197)
Browse files Browse the repository at this point in the history
  • Loading branch information
LukaszRozmej authored and kamilchodola committed Jun 20, 2024
1 parent 372ec53 commit 87706e9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
22 changes: 20 additions & 2 deletions src/Nethermind/Nethermind.State.Test/StorageProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Nethermind.Core.Crypto;
using Nethermind.Core.Extensions;
using Nethermind.Core.Resettables;
using Nethermind.Core.Test.Builders;
using Nethermind.Db;
using Nethermind.Specs.Forks;
using Nethermind.Logging;
Expand Down Expand Up @@ -409,16 +410,33 @@ public void Persistent_state_restores_independent_of_transient_state(int snapsho
_values[snapshot + 1].Should().BeEquivalentTo(provider.Get(new StorageCell(ctx.Address1, 1)).ToArray());
}

/// <summary>
/// Reset will reset transient state
/// </summary>
[Test]
public void Selfdestruct_clears_cache()
{
PreBlockCaches preBlockCaches = new PreBlockCaches();
Context ctx = new(preBlockCaches);
WorldState provider = BuildStorageProvider(ctx);
StorageCell storageCell = new StorageCell(TestItem.AddressA, 1);
preBlockCaches.StorageCache[storageCell] = [1, 2, 3];
provider.Get(storageCell);
provider.Commit(Paris.Instance);
provider.ClearStorage(TestItem.AddressA);
provider.Get(storageCell).ToArray().Should().BeEquivalentTo(StorageTree.EmptyBytes);
}

private class Context
{
public WorldState StateProvider { get; }

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

public Context()
public Context(PreBlockCaches preBlockCaches = null)
{
StateProvider = new WorldState(new TrieStore(new MemDb(), LimboLogs.Instance), Substitute.For<IDb>(), LogManager);
StateProvider = new WorldState(new TrieStore(new MemDb(), LimboLogs.Instance), Substitute.For<IDb>(), LogManager, preBlockCaches);
StateProvider.CreateAccount(Address1, 0);
StateProvider.CreateAccount(Address2, 0);
StateProvider.Commit(Frontier.Instance);
Expand Down
8 changes: 7 additions & 1 deletion src/Nethermind/Nethermind.State/PersistentStorageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,13 @@ public override void ClearStorage(Address address)
base.ClearStorage(address);

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

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

0 comments on commit 87706e9

Please sign in to comment.