From fb2a395202a055fd22ae245028b02fb0cff13f29 Mon Sep 17 00:00:00 2001 From: Klaudia Jazgar <67744705+kjazgar@users.noreply.github.com> Date: Wed, 16 Mar 2022 12:52:23 +0100 Subject: [PATCH] Fix/wrong startup logic (#3882) * Split StartBlockProducer into two classes * Refactored code. * Refactored code. --- .../Steps/InitializeBlockProducer.cs | 81 +++++++++++++++++++ .../Steps/RegisterRpcModules.cs | 2 +- .../Steps/StartBlockProducer.cs | 40 +-------- 3 files changed, 85 insertions(+), 38 deletions(-) create mode 100644 src/Nethermind/Nethermind.Init/Steps/InitializeBlockProducer.cs diff --git a/src/Nethermind/Nethermind.Init/Steps/InitializeBlockProducer.cs b/src/Nethermind/Nethermind.Init/Steps/InitializeBlockProducer.cs new file mode 100644 index 00000000000..f22fb5785fb --- /dev/null +++ b/src/Nethermind/Nethermind.Init/Steps/InitializeBlockProducer.cs @@ -0,0 +1,81 @@ +// Copyright (c) 2021 Demerzel Solutions Limited +// This file is part of the Nethermind library. +// +// The Nethermind library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The Nethermind library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the Nethermind. If not, see . + +using System; +using System.Threading; +using System.Threading.Tasks; +using Nethermind.Api; +using Nethermind.Api.Extensions; +using Nethermind.Blockchain; +using Nethermind.Blockchain.Producers; +using Nethermind.Consensus; +using Nethermind.Logging; + +namespace Nethermind.Init.Steps +{ + [RunnerStepDependencies(typeof(StartBlockProcessor), typeof(SetupKeyStore))] + public class InitializeBlockProducer : IStep + { + protected IApiWithBlockchain _api; + + public InitializeBlockProducer(INethermindApi api) + { + _api = api; + } + + public async Task Execute(CancellationToken _) + { + IMiningConfig miningConfig = _api.Config(); + if (miningConfig.Enabled) + { + _api.BlockProducer = await BuildProducer(); + } + } + + protected virtual async Task BuildProducer() + { + _api.BlockProducerEnvFactory = new BlockProducerEnvFactory(_api.DbProvider, + _api.BlockTree, + _api.ReadOnlyTrieStore, + _api.SpecProvider, + _api.BlockValidator, + _api.RewardCalculatorSource, + _api.ReceiptStorage, + _api.BlockPreprocessor, + _api.TxPool, + _api.TransactionComparerProvider, + _api.Config(), + _api.LogManager); + + if (_api.ChainSpec == null) throw new StepDependencyException(nameof(_api.ChainSpec)); + IConsensusPlugin? consensusPlugin = _api.GetConsensusPlugin(); + + if (consensusPlugin is not null) + { + foreach (IConsensusWrapperPlugin wrapperPlugin in _api.GetConsensusWrapperPlugins()) + { + return await wrapperPlugin.InitBlockProducer(consensusPlugin); + } + + return await consensusPlugin.InitBlockProducer(); + } + else + { + throw new NotSupportedException($"Mining in {_api.ChainSpec.SealEngineType} mode is not supported"); + } + } + } +} diff --git a/src/Nethermind/Nethermind.Init/Steps/RegisterRpcModules.cs b/src/Nethermind/Nethermind.Init/Steps/RegisterRpcModules.cs index 9467355e471..5ed2f84b3fa 100644 --- a/src/Nethermind/Nethermind.Init/Steps/RegisterRpcModules.cs +++ b/src/Nethermind/Nethermind.Init/Steps/RegisterRpcModules.cs @@ -46,7 +46,7 @@ namespace Nethermind.Init.Steps { - [RunnerStepDependencies(typeof(InitializeNetwork), typeof(SetupKeyStore), typeof(InitializeBlockchain), typeof(InitializePlugins))] + [RunnerStepDependencies(typeof(InitializeNetwork), typeof(SetupKeyStore), typeof(InitializeBlockchain), typeof(InitializePlugins), typeof(InitializeBlockProducer))] public class RegisterRpcModules : IStep { private readonly INethermindApi _api; diff --git a/src/Nethermind/Nethermind.Init/Steps/StartBlockProducer.cs b/src/Nethermind/Nethermind.Init/Steps/StartBlockProducer.cs index 8d63bac0412..baf8de357fd 100644 --- a/src/Nethermind/Nethermind.Init/Steps/StartBlockProducer.cs +++ b/src/Nethermind/Nethermind.Init/Steps/StartBlockProducer.cs @@ -26,7 +26,7 @@ namespace Nethermind.Init.Steps { - [RunnerStepDependencies(typeof(StartBlockProcessor), typeof(SetupKeyStore), typeof(ReviewBlockTree))] + [RunnerStepDependencies(typeof(InitializeBlockProducer), typeof(ReviewBlockTree))] public class StartBlockProducer : IStep { protected IApiWithBlockchain _api; @@ -41,49 +41,15 @@ public async Task Execute(CancellationToken _) IMiningConfig miningConfig = _api.Config(); if (miningConfig.Enabled) { - _api.BlockProducer = await BuildProducer(); - if (_api.BlockProducer == null) throw new StepDependencyException(nameof(_api.BlockProducer)); if (_api.BlockTree == null) throw new StepDependencyException(nameof(_api.BlockTree)); - + ILogger logger = _api.LogManager.GetClassLogger(); if (logger.IsWarn) logger.Warn($"Starting {_api.SealEngineType} block producer & sealer"); ProducedBlockSuggester suggester = new(_api.BlockTree, _api.BlockProducer); _api.DisposeStack.Push(suggester); - _api.BlockProducer.Start(); - } - } - - protected virtual async Task BuildProducer() - { - _api.BlockProducerEnvFactory = new BlockProducerEnvFactory(_api.DbProvider, - _api.BlockTree, - _api.ReadOnlyTrieStore, - _api.SpecProvider, - _api.BlockValidator, - _api.RewardCalculatorSource, - _api.ReceiptStorage, - _api.BlockPreprocessor, - _api.TxPool, - _api.TransactionComparerProvider, - _api.Config(), - _api.LogManager); - - if (_api.ChainSpec == null) throw new StepDependencyException(nameof(_api.ChainSpec)); - IConsensusPlugin? consensusPlugin = _api.GetConsensusPlugin(); - if (consensusPlugin is not null) - { - foreach (IConsensusWrapperPlugin wrapperPlugin in _api.GetConsensusWrapperPlugins()) - { - return await wrapperPlugin.InitBlockProducer(consensusPlugin); - } - - return await consensusPlugin.InitBlockProducer(); - } - else - { - throw new NotSupportedException($"Mining in {_api.ChainSpec.SealEngineType} mode is not supported"); + _api.BlockProducer.Start(); } } }