Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/wrong startup logic #3882

Merged
merged 3 commits into from
Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions src/Nethermind/Nethermind.Init/Steps/InitializeBlockProducer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// 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 <http://www.gnu.org/licenses/>.

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<IMiningConfig>();
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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move those to StartBlockProducer

}
}

protected virtual async Task<IBlockProducer> 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<IMiningConfig>(),
_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");
}
}
}
}
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Init/Steps/RegisterRpcModules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
50 changes: 8 additions & 42 deletions src/Nethermind/Nethermind.Init/Steps/StartBlockProducer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -41,49 +41,15 @@ public async Task Execute(CancellationToken _)
IMiningConfig miningConfig = _api.Config<IMiningConfig>();
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<IBlockProducer> 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<IMiningConfig>(),
_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())
if (_api.BlockProducer != null && _api.BlockTree != null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move StepDependencyExceptions here instead of this if

{
return await wrapperPlugin.InitBlockProducer(consensusPlugin);
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();
}

return await consensusPlugin.InitBlockProducer();
}
else
{
throw new NotSupportedException($"Mining in {_api.ChainSpec.SealEngineType} mode is not supported");
}
}
}
Expand Down