diff --git a/src/Nethermind/Nethermind.Init/Steps/RegisterPluginRpcModules.cs b/src/Nethermind/Nethermind.Init/Steps/RegisterPluginRpcModules.cs index 04d683529dd0..8999fcfe0d90 100644 --- a/src/Nethermind/Nethermind.Init/Steps/RegisterPluginRpcModules.cs +++ b/src/Nethermind/Nethermind.Init/Steps/RegisterPluginRpcModules.cs @@ -3,36 +3,23 @@ using System.Threading; using System.Threading.Tasks; -using Nethermind.Api; using Nethermind.Api.Extensions; using Nethermind.Api.Steps; -using Nethermind.Consensus.Processing; using Nethermind.Consensus.Producers; using Nethermind.JsonRpc.Modules; using Nethermind.JsonRpc.Modules.Evm; namespace Nethermind.Init.Steps; -[RunnerStepDependencies(typeof(InitializeNetwork), typeof(SetupKeyStore), typeof(InitializeBlockchain), typeof(InitializePlugins), typeof(InitializeBlockProducer), typeof(RegisterRpcModules))] +[RunnerStepDependencies(typeof(SetupKeyStore), typeof(InitializeBlockchain), typeof(InitializePlugins), typeof(InitializeBlockProducer), typeof(RegisterRpcModules))] public class RegisterPluginRpcModules( IRpcModuleProvider rpcModuleProvider, - IInitConfig initConfig, - IBlockProcessingQueue blockProcessingQueue, INethermindPlugin[] plugins, IManualBlockProductionTrigger manualBlockProductionTrigger ) : IStep { public virtual async Task Execute(CancellationToken cancellationToken) { - if (!initConfig.InRunnerTest) - { - // Ugly temporary hack to not receive engine API messages before end of processing of all blocks after restart. - // Then we will wait 5s more to ensure everything is processed - while (!blockProcessingQueue!.IsEmpty) - await Task.Delay(100); - await Task.Delay(5000); - } - foreach (INethermindPlugin plugin in plugins) { await plugin.InitRpcModules(); diff --git a/src/Nethermind/Nethermind.Init/Steps/ReviewBlockTree.cs b/src/Nethermind/Nethermind.Init/Steps/ReviewBlockTree.cs index 391879f4e15e..74abe0545993 100644 --- a/src/Nethermind/Nethermind.Init/Steps/ReviewBlockTree.cs +++ b/src/Nethermind/Nethermind.Init/Steps/ReviewBlockTree.cs @@ -1,6 +1,7 @@ // SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited // SPDX-License-Identifier: LGPL-3.0-only +using System; using System.Threading; using System.Threading.Tasks; using Nethermind.Api; @@ -8,6 +9,7 @@ using Nethermind.Blockchain; using Nethermind.Blockchain.Synchronization; using Nethermind.Blockchain.Visitors; +using Nethermind.Consensus.Processing; using Nethermind.Logging; using Nethermind.State; @@ -18,6 +20,7 @@ public class ReviewBlockTree( IWorldStateManager worldStateManager, IInitConfig initConfig, ISyncConfig syncConfig, + IBlockProcessingQueue blockProcessingQueue, IBlockTree blockTree, ILogManager logManager ) : IStep @@ -68,6 +71,20 @@ await blockTree.Accept(fixer, cancellationToken).ContinueWith(t => } }); } + + blockProcessingQueue.ProcessingQueueEmpty += OnProcessingQueueEmpty; + if (!blockProcessingQueue.IsEmpty) // Just in case the queue got empty before we subscribed + { + await _blocksProcessedTaskSource.Task.WaitAsync(cancellationToken); + } + blockProcessingQueue.ProcessingQueueEmpty -= OnProcessingQueueEmpty; + } + + private readonly TaskCompletionSource _blocksProcessedTaskSource = new(); + + private void OnProcessingQueueEmpty(object? sender, EventArgs e) + { + _blocksProcessedTaskSource.SetResult(); } } }