diff --git a/src/Nethermind/Nethermind.Merge.Plugin/BlockProduction/PayloadPreparationService.cs b/src/Nethermind/Nethermind.Merge.Plugin/BlockProduction/PayloadPreparationService.cs index 84b2c983a6c..6643f9d9b08 100644 --- a/src/Nethermind/Nethermind.Merge.Plugin/BlockProduction/PayloadPreparationService.cs +++ b/src/Nethermind/Nethermind.Merge.Plugin/BlockProduction/PayloadPreparationService.cs @@ -12,7 +12,6 @@ using Nethermind.Core.Crypto; using Nethermind.Core.Extensions; using Nethermind.Core.Timers; -using Nethermind.Int256; using Nethermind.Logging; using Nethermind.Merge.Plugin.Handlers; @@ -29,7 +28,6 @@ public class PayloadPreparationService : IPayloadPreparationService private readonly PostMergeBlockProducer _blockProducer; private readonly IBlockImprovementContextFactory _blockImprovementContextFactory; private readonly ILogger _logger; - private readonly List _payloadsToRemove = new(); // by default we will cleanup the old payload once per six slot. There is no need to fire it more often public const int SlotsPerOldPayloadCleanup = 6; @@ -151,28 +149,31 @@ private IBlockImprovementContext CreateBlockImprovementContext(string payloadId, private void CleanupOldPayloads(object? sender, EventArgs e) { - if (_logger.IsTrace) _logger.Trace("Started old payloads cleanup"); - foreach (KeyValuePair payload in _payloadStorage) + try { - DateTimeOffset now = DateTimeOffset.UtcNow; - if (payload.Value.StartDateTime + _cleanupOldPayloadDelay <= now) + if (_logger.IsTrace) _logger.Trace("Started old payloads cleanup"); + foreach (KeyValuePair payload in _payloadStorage) { - if (_logger.IsDebug) _logger.Info($"A new payload to remove: {payload.Key}, Current time {now:t}, Payload timestamp: {payload.Value.CurrentBestBlock?.Timestamp}"); - _payloadsToRemove.Add(payload.Key); + DateTimeOffset now = DateTimeOffset.UtcNow; + if (payload.Value.StartDateTime + _cleanupOldPayloadDelay <= now) + { + if (_logger.IsDebug) _logger.Info($"A new payload to remove: {payload.Key}, Current time {now:t}, Payload timestamp: {payload.Value.CurrentBestBlock?.Timestamp}"); + + if (_payloadStorage.TryRemove(payload.Key, out IBlockImprovementContext? context)) + { + context.Dispose(); + if (_logger.IsDebug) _logger.Info($"Cleaned up payload with id={payload.Key} as it was not requested"); + } + } } - } - foreach (string payloadToRemove in _payloadsToRemove) + if (_logger.IsTrace) _logger.Trace($"Finished old payloads cleanup"); + } + catch (Exception ex) { - if (_payloadStorage.TryRemove(payloadToRemove, out IBlockImprovementContext? context)) - { - context.Dispose(); - if (_logger.IsDebug) _logger.Info($"Cleaned up payload with id={payloadToRemove} as it was not requested"); - } + _logger.Error($"Exception in old payloads cleanup: {ex}"); } - _payloadsToRemove.Clear(); - if (_logger.IsTrace) _logger.Trace($"Finished old payloads cleanup"); } private Block? LogProductionResult(Task t)