Skip to content

Commit

Permalink
Remove coupling from merge plugin and aura (#7044)
Browse files Browse the repository at this point in the history
Co-authored-by: Ahmad Bitar <amb014@diyarme.com>
  • Loading branch information
smartprogrammer93 and Ahmad Bitar authored Aug 24, 2024
1 parent ad86f41 commit e856de5
Show file tree
Hide file tree
Showing 15 changed files with 91 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
Expand All @@ -11,13 +10,12 @@
using Nethermind.Consensus;
using Nethermind.Consensus.AuRa;
using Nethermind.Consensus.AuRa.Contracts;
using Nethermind.Consensus.AuRa.Withdrawals;
using Nethermind.Consensus.Processing;
using Nethermind.Consensus.Rewards;
using Nethermind.Consensus.Validators;
using Nethermind.Consensus.Withdrawals;
using Nethermind.Core;
using Nethermind.Logging;
using Nethermind.Trie.Pruning;
using NUnit.Framework;

namespace Nethermind.AuRa.Test.Contract;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
using Nethermind.Consensus.AuRa;
using Nethermind.Consensus.AuRa.Contracts;
using Nethermind.Consensus.AuRa.Transactions;
using Nethermind.Consensus.AuRa.Withdrawals;
using Nethermind.Consensus.Processing;
using Nethermind.Consensus.Rewards;
using Nethermind.Consensus.Transactions;
using Nethermind.Consensus.Validators;
using Nethermind.Consensus.Withdrawals;
using Nethermind.Core;
using Nethermind.Core.Specs;
using Nethermind.Core.Test.Builders;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
using Nethermind.Consensus.AuRa;
using Nethermind.Consensus.AuRa.Contracts;
using Nethermind.Consensus.AuRa.Transactions;
using Nethermind.Consensus.AuRa.Withdrawals;
using Nethermind.Consensus.Processing;
using Nethermind.Consensus.Rewards;
using Nethermind.Consensus.Validators;
using Nethermind.Consensus.Withdrawals;
using Nethermind.Core;
using Nethermind.Core.Caching;
using Nethermind.Core.Crypto;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
using Nethermind.Consensus.AuRa.Services;
using Nethermind.Consensus.AuRa.Transactions;
using Nethermind.Consensus.AuRa.Validators;
using Nethermind.Consensus.AuRa.Withdrawals;
using Nethermind.Consensus.Comparers;
using Nethermind.Consensus.Processing;
using Nethermind.Consensus.Transactions;
using Nethermind.Consensus.Validators;
using Nethermind.Consensus.Withdrawals;
using Nethermind.Core;
using Nethermind.Init.Steps;
using Nethermind.Logging;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
using Nethermind.Consensus.AuRa.Contracts.DataStore;
using Nethermind.Consensus.AuRa.Transactions;
using Nethermind.Consensus.AuRa.Validators;
using Nethermind.Consensus.AuRa.Withdrawals;
using Nethermind.Consensus.Processing;
using Nethermind.Consensus.Producers;
using Nethermind.Consensus.Transactions;
using Nethermind.Consensus.Withdrawals;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Crypto;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using Nethermind.Consensus.Withdrawals;
using Nethermind.Core;
using Nethermind.Core.Specs;

namespace Nethermind.Consensus.AuRa.Withdrawals;
namespace Nethermind.Consensus.Withdrawals;

public class NullWithdrawalProcessor : IWithdrawalProcessor
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using Nethermind.Blockchain;
using Nethermind.Consensus;
using Nethermind.Consensus.AuRa;
using Nethermind.Consensus.Processing;
using Nethermind.Core.Crypto;

namespace Nethermind.Merge.Plugin;

public class AuRaMergeFinalizationManager : MergeFinalizationManager, IAuRaBlockFinalizationManager
{
private readonly IAuRaBlockFinalizationManager _auRaBlockFinalizationManager;

public AuRaMergeFinalizationManager(IManualBlockFinalizationManager manualBlockFinalizationManager, IAuRaBlockFinalizationManager blockFinalizationManager, IPoSSwitcher poSSwitcher)
: base(manualBlockFinalizationManager, blockFinalizationManager, poSSwitcher)
{
_auRaBlockFinalizationManager = blockFinalizationManager;
_auRaBlockFinalizationManager.BlocksFinalized += OnBlockFinalized;
}

public long GetLastLevelFinalizedBy(Hash256 blockHash)
{
return _auRaBlockFinalizationManager.GetLastLevelFinalizedBy(blockHash);
}

public long? GetFinalizationLevel(long level)
{
return _auRaBlockFinalizationManager.GetFinalizationLevel(level);
}

public void SetMainBlockProcessor(IBlockProcessor blockProcessor)
{
_auRaBlockFinalizationManager.SetMainBlockProcessor(blockProcessor);
}

public override long LastFinalizedBlockLevel
{
get
{
return IsPostMerge
? _manualBlockFinalizationManager.LastFinalizedBlockLevel
: _auRaBlockFinalizationManager.LastFinalizedBlockLevel;
}
}

public override void Dispose()
{
if (IsPostMerge)
{
_auRaBlockFinalizationManager.Dispose();
}
base.Dispose();
}

}
15 changes: 12 additions & 3 deletions src/Nethermind/Nethermind.Merge.AuRa/AuRaMergePlugin.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Threading.Tasks;
using Nethermind.Api;
using Nethermind.Api.Extensions;
using Nethermind.Blockchain;
using Nethermind.Config;
using Nethermind.Consensus;
using Nethermind.Consensus.AuRa.Config;
using Nethermind.Consensus.AuRa.InitializationSteps;
using Nethermind.Consensus.AuRa.Transactions;
using Nethermind.Consensus.Producers;
using Nethermind.Consensus.Transactions;
using Nethermind.Core;
using Nethermind.Merge.Plugin;
Expand Down Expand Up @@ -49,7 +49,7 @@ originalFilter is MinGasPriceContractTxFilter ? originalFilter
public override IBlockProducer InitBlockProducer(IBlockProducerFactory consensusPlugin, ITxSource? txSource)
{
_api.BlockProducerEnvFactory = new AuRaMergeBlockProducerEnvFactory(
(AuRaNethermindApi)_api,
_auraApi!,
_api.WorldStateManager!,
_api.BlockTree!,
_api.SpecProvider!,
Expand All @@ -73,6 +73,15 @@ protected override PostMergeBlockProducerFactory CreateBlockProducerFactory()
_blocksConfig,
_api.LogManager);

protected override IBlockFinalizationManager InitializeMergeFinilizationManager()
{
return new AuRaMergeFinalizationManager(_blockFinalizationManager,
_auraApi!.FinalizationManager ??
throw new ArgumentNullException(nameof(_auraApi.FinalizationManager),
"Cannot instantiate AuRaMergeFinalizationManager when AuRaFinalizationManager is null!"),
_poSSwitcher);
}

public bool ShouldRunSteps(INethermindApi api)
{
_mergeConfig = api.Config<IMergeConfig>();
Expand Down
58 changes: 7 additions & 51 deletions src/Nethermind/Nethermind.Merge.Plugin/MergeFinalizationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,23 @@
using System;
using Nethermind.Blockchain;
using Nethermind.Consensus;
using Nethermind.Consensus.AuRa;
using Nethermind.Consensus.Processing;
using Nethermind.Core;
using Nethermind.Core.Crypto;

namespace Nethermind.Merge.Plugin
{
public class MergeFinalizationManager : IManualBlockFinalizationManager, IAuRaBlockFinalizationManager
public class MergeFinalizationManager : IManualBlockFinalizationManager
{
private readonly IManualBlockFinalizationManager _manualBlockFinalizationManager;
private readonly IAuRaBlockFinalizationManager? _auRaBlockFinalizationManager;
private bool HasAuRaFinalizationManager => _auRaBlockFinalizationManager is not null;
private bool IsPostMerge { get; set; }
protected readonly IManualBlockFinalizationManager _manualBlockFinalizationManager;
protected bool IsPostMerge { get; set; }

public event EventHandler<FinalizeEventArgs>? BlocksFinalized;

public MergeFinalizationManager(IManualBlockFinalizationManager manualBlockFinalizationManager,
IBlockFinalizationManager? blockFinalizationManager, IPoSSwitcher poSSwitcher)
{
_manualBlockFinalizationManager = manualBlockFinalizationManager;
_auRaBlockFinalizationManager = blockFinalizationManager as IAuRaBlockFinalizationManager;

poSSwitcher.TerminalBlockReached += OnSwitchHappened;
if (poSSwitcher.HasEverReachedTerminalBlock())
Expand All @@ -33,16 +29,14 @@ public MergeFinalizationManager(IManualBlockFinalizationManager manualBlockFinal
}

_manualBlockFinalizationManager.BlocksFinalized += OnBlockFinalized;
if (HasAuRaFinalizationManager)
_auRaBlockFinalizationManager!.BlocksFinalized += OnBlockFinalized;
}

private void OnSwitchHappened(object? sender, EventArgs e)
{
IsPostMerge = true;
}

private void OnBlockFinalized(object? sender, FinalizeEventArgs e)
protected void OnBlockFinalized(object? sender, FinalizeEventArgs e)
{
BlocksFinalized?.Invoke(this, e);
}
Expand All @@ -52,44 +46,9 @@ public void MarkFinalized(BlockHeader finalizingBlock, BlockHeader finalizedBloc
_manualBlockFinalizationManager.MarkFinalized(finalizingBlock, finalizedBlock);
}

public long GetLastLevelFinalizedBy(Hash256 blockHash)
{
if (_auRaBlockFinalizationManager is not null)
{
return _auRaBlockFinalizationManager.GetLastLevelFinalizedBy(blockHash);
}

throw new InvalidOperationException(
$"{nameof(GetLastLevelFinalizedBy)} called when empty {nameof(_auRaBlockFinalizationManager)} is null.");
}

public long? GetFinalizationLevel(long level)
{
if (_auRaBlockFinalizationManager is not null)
{
return _auRaBlockFinalizationManager.GetFinalizationLevel(level);
}

throw new InvalidOperationException(
$"{nameof(GetFinalizationLevel)} called when empty {nameof(_auRaBlockFinalizationManager)} is null.");
}

public void SetMainBlockProcessor(IBlockProcessor blockProcessor)
{
_auRaBlockFinalizationManager!.SetMainBlockProcessor(blockProcessor);
}

public void Dispose()
{
if (IsPostMerge && HasAuRaFinalizationManager)
{
_auRaBlockFinalizationManager!.Dispose();
}
}

public Hash256 LastFinalizedHash { get => _manualBlockFinalizationManager.LastFinalizedHash; }

public long LastFinalizedBlockLevel
public virtual long LastFinalizedBlockLevel
{
get
{
Expand All @@ -98,13 +57,10 @@ public long LastFinalizedBlockLevel
return _manualBlockFinalizationManager.LastFinalizedBlockLevel;
}

if (HasAuRaFinalizationManager)
{
return _auRaBlockFinalizationManager!.LastFinalizedBlockLevel;
}

return 0;
}
}

public virtual void Dispose() { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Threading.Tasks;
using Nethermind.Consensus;
using Nethermind.Consensus.Transactions;
using Nethermind.Core;
Expand Down
10 changes: 7 additions & 3 deletions src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public partial class MergePlugin : IConsensusWrapperPlugin, ISynchronizationPlug
private InvalidChainTracker.InvalidChainTracker _invalidChainTracker = null!;
private IPeerRefresher _peerRefresher = null!;

private ManualBlockFinalizationManager _blockFinalizationManager = null!;
protected ManualBlockFinalizationManager _blockFinalizationManager = null!;
private IMergeBlockProductionPolicy? _mergeBlockProductionPolicy;

public virtual string Name => "Merge";
Expand Down Expand Up @@ -259,8 +259,7 @@ public Task InitNetworkProtocol()
new MergeHealthHintService(_api.HealthHintService, _poSSwitcher, _blocksConfig);
_mergeBlockProductionPolicy = new MergeBlockProductionPolicy(_api.BlockProductionPolicy);
_api.BlockProductionPolicy = _mergeBlockProductionPolicy;

_api.FinalizationManager = new MergeFinalizationManager(_blockFinalizationManager, _api.FinalizationManager, _poSSwitcher);
_api.FinalizationManager = InitializeMergeFinilizationManager();

// Need to do it here because blockprocessor is not available in init
_invalidChainTracker.SetupBlockchainProcessorInterceptor(_api.BlockchainProcessor!);
Expand All @@ -269,6 +268,11 @@ public Task InitNetworkProtocol()
return Task.CompletedTask;
}

protected virtual IBlockFinalizationManager InitializeMergeFinilizationManager()
{
return new MergeFinalizationManager(_blockFinalizationManager, _api.FinalizationManager, _poSSwitcher);
}

public Task InitRpcModules()
{
if (MergeEnabled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

<ItemGroup>
<ProjectReference Include="..\Nethermind.Api\Nethermind.Api.csproj" />
<ProjectReference Include="..\Nethermind.Consensus.AuRa\Nethermind.Consensus.AuRa.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Nethermind.Blockchain.Blocks;
using Nethermind.Blockchain.Services;
using Nethermind.Config;
using Nethermind.Consensus.AuRa.Withdrawals;
using Nethermind.Consensus.Processing;
using Nethermind.Consensus.Producers;
using Nethermind.Consensus.Validators;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<ProjectReference Include="..\Nethermind.Blockchain\Nethermind.Blockchain.csproj" />
<ProjectReference Include="..\Nethermind.Core\Nethermind.Core.csproj" />
<ProjectReference Include="..\Nethermind.Consensus\Nethermind.Consensus.csproj" />
<ProjectReference Include="..\Nethermind.Init\Nethermind.Init.csproj" />
<ProjectReference Include="..\Nethermind.JsonRpc\Nethermind.JsonRpc.csproj" />
<ProjectReference Include="..\Nethermind.Merge.Plugin\Nethermind.Merge.Plugin.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Nethermind.Api;
using Nethermind.Blockchain;
using Nethermind.Config;
using Nethermind.Consensus.AuRa.Withdrawals;
using Nethermind.Consensus.Withdrawals;
using Nethermind.Init.Steps;
using Nethermind.JsonRpc;
Expand Down

0 comments on commit e856de5

Please sign in to comment.