From e856de5a33259ea0e54c40c28db37631bf56c2c0 Mon Sep 17 00:00:00 2001 From: Ahmad Bitar <33181301+smartprogrammer93@users.noreply.github.com> Date: Sat, 24 Aug 2024 20:03:14 +0300 Subject: [PATCH] Remove coupling from merge plugin and aura (#7044) Co-authored-by: Ahmad Bitar --- .../AuRaContractGasLimitOverrideTests.cs | 4 +- .../Transactions/TxCertifierFilterTests.cs | 2 +- .../Transactions/TxPermissionFilterTest.cs | 2 +- .../InitializeBlockchainAuRa.cs | 2 +- .../StartBlockProducerAuRa.cs | 2 +- .../Withdrawals/NullWithdrawalProcessor.cs | 3 +- .../AuRaMergeFinalizationManager.cs | 58 +++++++++++++++++++ .../Nethermind.Merge.AuRa/AuRaMergePlugin.cs | 15 ++++- .../MergeFinalizationManager.cs | 58 +++---------------- .../MergePlugin.BlockProducer.cs | 1 - .../Nethermind.Merge.Plugin/MergePlugin.cs | 10 +++- .../Nethermind.Merge.Plugin.csproj | 1 - .../InitializeBlockchainOptimism.cs | 1 - .../Nethermind.Optimism.csproj | 1 + .../Rpc/RegisterOptimismRpcModules.cs | 1 - 15 files changed, 91 insertions(+), 70 deletions(-) rename src/Nethermind/{Nethermind.Consensus.AuRa => Nethermind.Consensus}/Withdrawals/NullWithdrawalProcessor.cs (81%) create mode 100644 src/Nethermind/Nethermind.Merge.AuRa/AuRaMergeFinalizationManager.cs diff --git a/src/Nethermind/Nethermind.AuRa.Test/Contract/AuRaContractGasLimitOverrideTests.cs b/src/Nethermind/Nethermind.AuRa.Test/Contract/AuRaContractGasLimitOverrideTests.cs index 7fc895f94c4..3e05e43fde1 100644 --- a/src/Nethermind/Nethermind.AuRa.Test/Contract/AuRaContractGasLimitOverrideTests.cs +++ b/src/Nethermind/Nethermind.AuRa.Test/Contract/AuRaContractGasLimitOverrideTests.cs @@ -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; @@ -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; diff --git a/src/Nethermind/Nethermind.AuRa.Test/Transactions/TxCertifierFilterTests.cs b/src/Nethermind/Nethermind.AuRa.Test/Transactions/TxCertifierFilterTests.cs index ae3c8a5d699..92a33ae4941 100644 --- a/src/Nethermind/Nethermind.AuRa.Test/Transactions/TxCertifierFilterTests.cs +++ b/src/Nethermind/Nethermind.AuRa.Test/Transactions/TxCertifierFilterTests.cs @@ -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; diff --git a/src/Nethermind/Nethermind.AuRa.Test/Transactions/TxPermissionFilterTest.cs b/src/Nethermind/Nethermind.AuRa.Test/Transactions/TxPermissionFilterTest.cs index 2ab52018fd9..dbaa774b2ff 100644 --- a/src/Nethermind/Nethermind.AuRa.Test/Transactions/TxPermissionFilterTest.cs +++ b/src/Nethermind/Nethermind.AuRa.Test/Transactions/TxPermissionFilterTest.cs @@ -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; diff --git a/src/Nethermind/Nethermind.Consensus.AuRa/InitializationSteps/InitializeBlockchainAuRa.cs b/src/Nethermind/Nethermind.Consensus.AuRa/InitializationSteps/InitializeBlockchainAuRa.cs index 9253574e72a..63b74023691 100644 --- a/src/Nethermind/Nethermind.Consensus.AuRa/InitializationSteps/InitializeBlockchainAuRa.cs +++ b/src/Nethermind/Nethermind.Consensus.AuRa/InitializationSteps/InitializeBlockchainAuRa.cs @@ -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; diff --git a/src/Nethermind/Nethermind.Consensus.AuRa/InitializationSteps/StartBlockProducerAuRa.cs b/src/Nethermind/Nethermind.Consensus.AuRa/InitializationSteps/StartBlockProducerAuRa.cs index ee84c5f21a2..948eaef9a97 100644 --- a/src/Nethermind/Nethermind.Consensus.AuRa/InitializationSteps/StartBlockProducerAuRa.cs +++ b/src/Nethermind/Nethermind.Consensus.AuRa/InitializationSteps/StartBlockProducerAuRa.cs @@ -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; diff --git a/src/Nethermind/Nethermind.Consensus.AuRa/Withdrawals/NullWithdrawalProcessor.cs b/src/Nethermind/Nethermind.Consensus/Withdrawals/NullWithdrawalProcessor.cs similarity index 81% rename from src/Nethermind/Nethermind.Consensus.AuRa/Withdrawals/NullWithdrawalProcessor.cs rename to src/Nethermind/Nethermind.Consensus/Withdrawals/NullWithdrawalProcessor.cs index 24f84f52ba2..24581691fcf 100644 --- a/src/Nethermind/Nethermind.Consensus.AuRa/Withdrawals/NullWithdrawalProcessor.cs +++ b/src/Nethermind/Nethermind.Consensus/Withdrawals/NullWithdrawalProcessor.cs @@ -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 { diff --git a/src/Nethermind/Nethermind.Merge.AuRa/AuRaMergeFinalizationManager.cs b/src/Nethermind/Nethermind.Merge.AuRa/AuRaMergeFinalizationManager.cs new file mode 100644 index 00000000000..db08643160e --- /dev/null +++ b/src/Nethermind/Nethermind.Merge.AuRa/AuRaMergeFinalizationManager.cs @@ -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(); + } + +} diff --git a/src/Nethermind/Nethermind.Merge.AuRa/AuRaMergePlugin.cs b/src/Nethermind/Nethermind.Merge.AuRa/AuRaMergePlugin.cs index ac61be06612..5e6b55a5ff4 100644 --- a/src/Nethermind/Nethermind.Merge.AuRa/AuRaMergePlugin.cs +++ b/src/Nethermind/Nethermind.Merge.AuRa/AuRaMergePlugin.cs @@ -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; @@ -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!, @@ -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(); diff --git a/src/Nethermind/Nethermind.Merge.Plugin/MergeFinalizationManager.cs b/src/Nethermind/Nethermind.Merge.Plugin/MergeFinalizationManager.cs index 73ae1da48b3..90c3a276ae4 100644 --- a/src/Nethermind/Nethermind.Merge.Plugin/MergeFinalizationManager.cs +++ b/src/Nethermind/Nethermind.Merge.Plugin/MergeFinalizationManager.cs @@ -4,19 +4,16 @@ 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? BlocksFinalized; @@ -24,7 +21,6 @@ public MergeFinalizationManager(IManualBlockFinalizationManager manualBlockFinal IBlockFinalizationManager? blockFinalizationManager, IPoSSwitcher poSSwitcher) { _manualBlockFinalizationManager = manualBlockFinalizationManager; - _auRaBlockFinalizationManager = blockFinalizationManager as IAuRaBlockFinalizationManager; poSSwitcher.TerminalBlockReached += OnSwitchHappened; if (poSSwitcher.HasEverReachedTerminalBlock()) @@ -33,8 +29,6 @@ public MergeFinalizationManager(IManualBlockFinalizationManager manualBlockFinal } _manualBlockFinalizationManager.BlocksFinalized += OnBlockFinalized; - if (HasAuRaFinalizationManager) - _auRaBlockFinalizationManager!.BlocksFinalized += OnBlockFinalized; } private void OnSwitchHappened(object? sender, EventArgs e) @@ -42,7 +36,7 @@ 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); } @@ -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 { @@ -98,13 +57,10 @@ public long LastFinalizedBlockLevel return _manualBlockFinalizationManager.LastFinalizedBlockLevel; } - if (HasAuRaFinalizationManager) - { - return _auRaBlockFinalizationManager!.LastFinalizedBlockLevel; - } - return 0; } } + + public virtual void Dispose() { } } } diff --git a/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.BlockProducer.cs b/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.BlockProducer.cs index 2ad0d4f412e..d9523f91164 100644 --- a/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.BlockProducer.cs +++ b/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.BlockProducer.cs @@ -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; diff --git a/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs b/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs index 2adf42baa54..774eebf1d20 100644 --- a/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs +++ b/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs @@ -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"; @@ -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!); @@ -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) diff --git a/src/Nethermind/Nethermind.Merge.Plugin/Nethermind.Merge.Plugin.csproj b/src/Nethermind/Nethermind.Merge.Plugin/Nethermind.Merge.Plugin.csproj index c6647fe4819..a8dfde638f6 100644 --- a/src/Nethermind/Nethermind.Merge.Plugin/Nethermind.Merge.Plugin.csproj +++ b/src/Nethermind/Nethermind.Merge.Plugin/Nethermind.Merge.Plugin.csproj @@ -7,7 +7,6 @@ - diff --git a/src/Nethermind/Nethermind.Optimism/InitializeBlockchainOptimism.cs b/src/Nethermind/Nethermind.Optimism/InitializeBlockchainOptimism.cs index 92ab38d4aad..6c42963e032 100644 --- a/src/Nethermind/Nethermind.Optimism/InitializeBlockchainOptimism.cs +++ b/src/Nethermind/Nethermind.Optimism/InitializeBlockchainOptimism.cs @@ -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; diff --git a/src/Nethermind/Nethermind.Optimism/Nethermind.Optimism.csproj b/src/Nethermind/Nethermind.Optimism/Nethermind.Optimism.csproj index 53cf806f73d..939e7284adb 100644 --- a/src/Nethermind/Nethermind.Optimism/Nethermind.Optimism.csproj +++ b/src/Nethermind/Nethermind.Optimism/Nethermind.Optimism.csproj @@ -10,6 +10,7 @@ + diff --git a/src/Nethermind/Nethermind.Optimism/Rpc/RegisterOptimismRpcModules.cs b/src/Nethermind/Nethermind.Optimism/Rpc/RegisterOptimismRpcModules.cs index 360ccaa1eda..04da5fca22e 100644 --- a/src/Nethermind/Nethermind.Optimism/Rpc/RegisterOptimismRpcModules.cs +++ b/src/Nethermind/Nethermind.Optimism/Rpc/RegisterOptimismRpcModules.cs @@ -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;