diff --git a/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.Setup.cs b/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.Setup.cs index 25b5bfe2f0f..8ada5ce1885 100644 --- a/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.Setup.cs +++ b/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.Setup.cs @@ -78,7 +78,7 @@ private IEngineRpcModule CreateEngineModule(MergeTestBlockchain chain, ISyncConf blockCacheService, chain.LogManager); invalidChainTracker.SetupBlockchainProcessorInterceptor(chain.BlockchainProcessor); - chain.BeaconSync = new BeaconSync(chain.BeaconPivot, chain.BlockTree, synchronizationConfig, blockCacheService, chain.LogManager); + chain.BeaconSync = new BeaconSync(chain.BeaconPivot, chain.BlockTree, synchronizationConfig, blockCacheService, chain.PoSSwitcher, chain.LogManager); EngineRpcCapabilitiesProvider capabilitiesProvider = new(chain.SpecProvider); return new EngineRpcModule( new GetPayloadV1Handler( diff --git a/src/Nethermind/Nethermind.Merge.Plugin.Test/Synchronization/BeaconHeadersSyncTests.cs b/src/Nethermind/Nethermind.Merge.Plugin.Test/Synchronization/BeaconHeadersSyncTests.cs index 1d9bdcbd1ae..3e7d4dadbee 100644 --- a/src/Nethermind/Nethermind.Merge.Plugin.Test/Synchronization/BeaconHeadersSyncTests.cs +++ b/src/Nethermind/Nethermind.Merge.Plugin.Test/Synchronization/BeaconHeadersSyncTests.cs @@ -65,7 +65,7 @@ public IBeaconPivot BeaconPivot } private BeaconSync? _beaconSync; - public BeaconSync BeaconSync => _beaconSync ??= new(BeaconPivot, BlockTree, SyncConfig, BlockCacheService, LimboLogs.Instance); + public BeaconSync BeaconSync => _beaconSync ??= new(BeaconPivot, BlockTree, SyncConfig, BlockCacheService, PoSSwitcher, LimboLogs.Instance); private IDb? _metadataDb; public IDb MetadataDb => _metadataDb ??= new MemDb(); diff --git a/src/Nethermind/Nethermind.Merge.Plugin/Handlers/BlockCacheService.cs b/src/Nethermind/Nethermind.Merge.Plugin/Handlers/BlockCacheService.cs index e411bcc211f..ad5bfdda95b 100644 --- a/src/Nethermind/Nethermind.Merge.Plugin/Handlers/BlockCacheService.cs +++ b/src/Nethermind/Nethermind.Merge.Plugin/Handlers/BlockCacheService.cs @@ -10,5 +10,5 @@ namespace Nethermind.Merge.Plugin.Handlers; public class BlockCacheService : IBlockCacheService { public ConcurrentDictionary BlockCache { get; } = new(); - public Hash256 FinalizedHash { get; set; } = Keccak.Zero; + public Hash256? FinalizedHash { get; set; } } diff --git a/src/Nethermind/Nethermind.Merge.Plugin/Handlers/IBlockCacheService.cs b/src/Nethermind/Nethermind.Merge.Plugin/Handlers/IBlockCacheService.cs index 77b5979cec5..191318bb5d7 100644 --- a/src/Nethermind/Nethermind.Merge.Plugin/Handlers/IBlockCacheService.cs +++ b/src/Nethermind/Nethermind.Merge.Plugin/Handlers/IBlockCacheService.cs @@ -10,5 +10,5 @@ namespace Nethermind.Merge.Plugin.Handlers; public interface IBlockCacheService { public ConcurrentDictionary BlockCache { get; } - Hash256 FinalizedHash { get; set; } + Hash256? FinalizedHash { get; set; } } diff --git a/src/Nethermind/Nethermind.Merge.Plugin/MergeGossipPolicy.cs b/src/Nethermind/Nethermind.Merge.Plugin/MergeGossipPolicy.cs index a3983e90070..28db239cf26 100644 --- a/src/Nethermind/Nethermind.Merge.Plugin/MergeGossipPolicy.cs +++ b/src/Nethermind/Nethermind.Merge.Plugin/MergeGossipPolicy.cs @@ -36,7 +36,8 @@ public MergeGossipPolicy( // According to spec (https://github.com/ethereum/EIPs/blob/d896145678bd65d3eafd8749690c1b5228875c39/EIPS/eip-3675.md#network) // We MUST discard NewBlock/NewBlockHash messages after receiving FIRST_FINALIZED_BLOCK. public bool ShouldDiscardBlocks => _poSSwitcher.TransitionFinished || - _blockCacheService.FinalizedHash != Keccak.Zero; /* _blockCacheService.FinalizedHash != Keccak.Zero - this condition was added for edge case situation. + (_blockCacheService.FinalizedHash != null && _blockCacheService.FinalizedHash != Keccak.Zero); /* _blockCacheService.FinalizedHash != null && _blockCacheService.FinalizedHash != Keccak.Zero + This condition was added for edge case situation. We started beacon sync, and we hadn't reached transition yet. If CL sent us non zero finalization hash, it would mean that network reached transition. However, in edge case situation (verified by merge hive tests), our node needs to be reorged to PoW again, so we can't add this condition _blockCacheService.FinalizedHash != Keccak.Zero to PoSSwitcher.TransitionFinished. On the other hand, we don't want to receive any blocks from the network, so we want to discard blocks. */ diff --git a/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs b/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs index eebb8fc2b17..e1ac8dd4c12 100644 --- a/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs +++ b/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs @@ -407,7 +407,7 @@ public Task InitSynchronization() _api.LogManager), _invalidChainTracker, _api.LogManager); - _beaconSync = new BeaconSync(_beaconPivot, _api.BlockTree, _syncConfig, _blockCacheService, _api.LogManager); + _beaconSync = new BeaconSync(_beaconPivot, _api.BlockTree, _syncConfig, _blockCacheService, _poSSwitcher, _api.LogManager); _api.BetterPeerStrategy = new MergeBetterPeerStrategy(_api.BetterPeerStrategy, _poSSwitcher, _beaconPivot, _api.LogManager); diff --git a/src/Nethermind/Nethermind.Merge.Plugin/Synchronization/BeaconSync.cs b/src/Nethermind/Nethermind.Merge.Plugin/Synchronization/BeaconSync.cs index 1591dbc18af..368c5d11516 100644 --- a/src/Nethermind/Nethermind.Merge.Plugin/Synchronization/BeaconSync.cs +++ b/src/Nethermind/Nethermind.Merge.Plugin/Synchronization/BeaconSync.cs @@ -3,6 +3,7 @@ using Nethermind.Blockchain; using Nethermind.Blockchain.Synchronization; +using Nethermind.Consensus; using Nethermind.Core; using Nethermind.Core.Crypto; using Nethermind.Crypto; @@ -18,6 +19,7 @@ public class BeaconSync : IMergeSyncController, IBeaconSyncStrategy private readonly IBlockTree _blockTree; private readonly ISyncConfig _syncConfig; private readonly IBlockCacheService _blockCacheService; + private readonly IPoSSwitcher _poSSwitcher; private bool _isInBeaconModeControl = false; private readonly ILogger _logger; @@ -26,12 +28,14 @@ public BeaconSync( IBlockTree blockTree, ISyncConfig syncConfig, IBlockCacheService blockCacheService, + IPoSSwitcher poSSwitcher, ILogManager logManager) { _beaconPivot = beaconPivot; _blockTree = blockTree; _syncConfig = syncConfig; _blockCacheService = blockCacheService; + _poSSwitcher = poSSwitcher; _logger = logManager.GetClassLogger(); } @@ -105,6 +109,8 @@ lowestInsertedBeaconHeader is not null && /// public bool IsBeaconSyncFinished(BlockHeader? blockHeader) => !_beaconPivot.BeaconPivotExists() || (blockHeader is not null && _blockTree.WasProcessed(blockHeader.Number, blockHeader.GetOrCalculateHash())); + public bool MergeTransitionFinished => _poSSwitcher.TransitionFinished; + public long? GetTargetBlockHeight() { if (_beaconPivot.BeaconPivotExists()) @@ -114,7 +120,7 @@ lowestInsertedBeaconHeader is not null && return null; } - public Hash256 GetFinalizedHash() + public Hash256? GetFinalizedHash() { return _blockCacheService.FinalizedHash; } diff --git a/src/Nethermind/Nethermind.Optimism/OptimismPlugin.cs b/src/Nethermind/Nethermind.Optimism/OptimismPlugin.cs index d95345a7fbb..c1897641cc1 100644 --- a/src/Nethermind/Nethermind.Optimism/OptimismPlugin.cs +++ b/src/Nethermind/Nethermind.Optimism/OptimismPlugin.cs @@ -136,7 +136,7 @@ public Task InitSynchronization() _api.DisposeStack.Push((PeerRefresher)_peerRefresher); _beaconPivot = new BeaconPivot(_syncConfig, _api.DbProvider.MetadataDb, _api.BlockTree, _api.LogManager); - _beaconSync = new BeaconSync(_beaconPivot, _api.BlockTree, _syncConfig, _blockCacheService, _api.LogManager); + _beaconSync = new BeaconSync(_beaconPivot, _api.BlockTree, _syncConfig, _blockCacheService, _api.PoSSwitcher, _api.LogManager); _api.BetterPeerStrategy = new MergeBetterPeerStrategy(null!, _api.PoSSwitcher, _beaconPivot, _api.LogManager); _api.Pivot = _beaconPivot; diff --git a/src/Nethermind/Nethermind.Synchronization.Test/ParallelSync/MultiSyncModeSelectorTests.Scenario.cs b/src/Nethermind/Nethermind.Synchronization.Test/ParallelSync/MultiSyncModeSelectorTests.Scenario.cs index b7cc95c5442..6e4e327bbe6 100644 --- a/src/Nethermind/Nethermind.Synchronization.Test/ParallelSync/MultiSyncModeSelectorTests.Scenario.cs +++ b/src/Nethermind/Nethermind.Synchronization.Test/ParallelSync/MultiSyncModeSelectorTests.Scenario.cs @@ -884,7 +884,7 @@ public ScenarioBuilder WhenMergeSyncPivotNotResolvedYet() { SyncConfig.MaxAttemptsToUpdatePivot = 3; BeaconSyncStrategy = Substitute.For(); - BeaconSyncStrategy.GetFinalizedHash().Returns(TestItem.KeccakA); + BeaconSyncStrategy.MergeTransitionFinished.Returns(true); return "merge sync pivot not resolved yet"; } ); diff --git a/src/Nethermind/Nethermind.Synchronization/IBeaconSyncStrategy.cs b/src/Nethermind/Nethermind.Synchronization/IBeaconSyncStrategy.cs index 264369d4ac7..9043437fe28 100644 --- a/src/Nethermind/Nethermind.Synchronization/IBeaconSyncStrategy.cs +++ b/src/Nethermind/Nethermind.Synchronization/IBeaconSyncStrategy.cs @@ -17,6 +17,7 @@ private No() { } public bool ShouldBeInBeaconModeControl() => false; public bool IsBeaconSyncFinished(BlockHeader? blockHeader) => true; + public bool MergeTransitionFinished => false; public long? GetTargetBlockHeight() => null; public Hash256? GetFinalizedHash() => null; } @@ -27,6 +28,8 @@ public interface IBeaconSyncStrategy bool ShouldBeInBeaconModeControl(); bool IsBeaconSyncFinished(BlockHeader? blockHeader); + public bool MergeTransitionFinished { get; } + public long? GetTargetBlockHeight(); public Hash256? GetFinalizedHash(); } diff --git a/src/Nethermind/Nethermind.Synchronization/ParallelSync/MultiSyncModeSelector.cs b/src/Nethermind/Nethermind.Synchronization/ParallelSync/MultiSyncModeSelector.cs index f7f7c40fcd0..8dc3378cbde 100644 --- a/src/Nethermind/Nethermind.Synchronization/ParallelSync/MultiSyncModeSelector.cs +++ b/src/Nethermind/Nethermind.Synchronization/ParallelSync/MultiSyncModeSelector.cs @@ -356,7 +356,7 @@ private bool ShouldBeInBeaconHeaders(bool shouldBeInUpdatingPivot) private bool ShouldBeInUpdatingPivot() { bool updateRequestedAndNotFinished = _syncConfig.MaxAttemptsToUpdatePivot > 0; - bool isPostMerge = _beaconSyncStrategy.GetFinalizedHash() != null; + bool isPostMerge = _beaconSyncStrategy.MergeTransitionFinished; bool stateSyncNotFinished = _syncProgressResolver.FindBestFullState() == 0; bool result = updateRequestedAndNotFinished &&