diff --git a/src/Nethermind/Nethermind.Consensus/Processing/BlockCachePreWarmer.cs b/src/Nethermind/Nethermind.Consensus/Processing/BlockCachePreWarmer.cs index 0c2c6455137..9f98ec99e23 100644 --- a/src/Nethermind/Nethermind.Consensus/Processing/BlockCachePreWarmer.cs +++ b/src/Nethermind/Nethermind.Consensus/Processing/BlockCachePreWarmer.cs @@ -30,9 +30,10 @@ public Task PreWarmCaches(Block suggestedBlock, Hash256? parentStateRoot, Access { if (preBlockCaches is not null) { - if (preBlockCaches.ClearCaches()) + CacheType result = preBlockCaches.ClearCaches(); + if (result != default) { - if (_logger.IsDebug) _logger.Debug("Caches are not empty. Clearing them."); + if (_logger.IsDebug) _logger.Debug($"Caches {result} are not empty. Clearing them."); } var physicalCoreCount = RuntimeInformation.PhysicalCoreCount; @@ -54,7 +55,7 @@ public Task PreWarmCaches(Block suggestedBlock, Hash256? parentStateRoot, Access // Parent state root is null for genesis block private static bool IsGenesisBlock(Hash256? parentStateRoot) => parentStateRoot is null; - public bool ClearCaches() => preBlockCaches?.ClearCaches() ?? false; + public CacheType ClearCaches() => preBlockCaches?.ClearCaches() ?? default; private void PreWarmCachesParallel(Block suggestedBlock, Hash256 parentStateRoot, ParallelOptions parallelOptions, AddressWarmer addressWarmer, CancellationToken cancellationToken) { diff --git a/src/Nethermind/Nethermind.Consensus/Processing/BlockProcessor.cs b/src/Nethermind/Nethermind.Consensus/Processing/BlockProcessor.cs index 3d2e62f7401..d1dedda198f 100644 --- a/src/Nethermind/Nethermind.Consensus/Processing/BlockProcessor.cs +++ b/src/Nethermind/Nethermind.Consensus/Processing/BlockProcessor.cs @@ -133,9 +133,10 @@ the previous head state.*/ } else { - if (preWarmer?.ClearCaches() ?? false) + CacheType result = preWarmer?.ClearCaches() ?? default; + if (result != default) { - if (_logger.IsDebug) _logger.Debug("Low txs, caches are not empty. Clearing them."); + if (_logger.IsDebug) _logger.Debug($"Low txs, caches {result} are not empty. Clearing them."); } // Even though we skip prewarming we still need to ensure the caches are cleared (processedBlock, receipts) = ProcessOne(suggestedBlock, options, blockTracer); diff --git a/src/Nethermind/Nethermind.Consensus/Processing/IBlockCachePreWarmer.cs b/src/Nethermind/Nethermind.Consensus/Processing/IBlockCachePreWarmer.cs index 3c1edd49bf0..206261d3f2f 100644 --- a/src/Nethermind/Nethermind.Consensus/Processing/IBlockCachePreWarmer.cs +++ b/src/Nethermind/Nethermind.Consensus/Processing/IBlockCachePreWarmer.cs @@ -1,16 +1,18 @@ // SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited // SPDX-License-Identifier: LGPL-3.0-only +using System; using System.Threading; using System.Threading.Tasks; using Nethermind.Core; using Nethermind.Core.Crypto; using Nethermind.Core.Eip2930; +using Nethermind.State; namespace Nethermind.Consensus.Processing; public interface IBlockCachePreWarmer { Task PreWarmCaches(Block suggestedBlock, Hash256 parentStateRoot, AccessList? systemTxAccessList, CancellationToken cancellationToken = default); - bool ClearCaches(); + CacheType ClearCaches(); } diff --git a/src/Nethermind/Nethermind.State/PreBlockCaches.cs b/src/Nethermind/Nethermind.State/PreBlockCaches.cs index 6021b984256..b379eca3d59 100644 --- a/src/Nethermind/Nethermind.State/PreBlockCaches.cs +++ b/src/Nethermind/Nethermind.State/PreBlockCaches.cs @@ -17,7 +17,7 @@ public class PreBlockCaches private const int InitialCapacity = 4096 * 8; private static int LockPartitions => CollectionExtensions.LockPartitions; - private readonly Func[] _clearCaches; + private readonly Func[] _clearCaches; private readonly ConcurrentDictionary _storageCache = new(LockPartitions, InitialCapacity); private readonly ConcurrentDictionary _stateCache = new(LockPartitions, InitialCapacity); @@ -28,10 +28,10 @@ public PreBlockCaches() { _clearCaches = [ - _storageCache.NoResizeClear, - _stateCache.NoResizeClear, - _rlpCache.NoResizeClear, - _precompileCache.NoResizeClear + () => _storageCache.NoResizeClear() ? CacheType.Storage : CacheType.None, + () => _stateCache.NoResizeClear() ? CacheType.State : CacheType.None, + () => _rlpCache.NoResizeClear() ? CacheType.Rlp : CacheType.None, + () => _precompileCache.NoResizeClear() ? CacheType.Precompile : CacheType.None ]; } @@ -40,10 +40,10 @@ public PreBlockCaches() public ConcurrentDictionary RlpCache => _rlpCache; public ConcurrentDictionary, bool)> PrecompileCache => _precompileCache; - public bool ClearCaches() + public CacheType ClearCaches() { - bool isDirty = false; - foreach (Func clearCache in _clearCaches) + CacheType isDirty = CacheType.None; + foreach (Func clearCache in _clearCaches) { isDirty |= clearCache(); } @@ -60,3 +60,13 @@ public readonly struct PrecompileCacheKey(Address address, ReadOnlyMemory public override int GetHashCode() => Data.Span.FastHash() ^ Address.GetHashCode(); } } + +[Flags] +public enum CacheType +{ + None = 0, + Storage = 0b1, + State = 0b10, + Rlp = 0b100, + Precompile = 0b1000 +}