Skip to content

Commit

Permalink
Changed post-merge condition in MultiSyncModeSelector (#6433)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarekM25 authored Jan 6, 2024
1 parent 9b5a3f2 commit 74743cd
Show file tree
Hide file tree
Showing 11 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ namespace Nethermind.Merge.Plugin.Handlers;
public class BlockCacheService : IBlockCacheService
{
public ConcurrentDictionary<Hash256, Block> BlockCache { get; } = new();
public Hash256 FinalizedHash { get; set; } = Keccak.Zero;
public Hash256? FinalizedHash { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ namespace Nethermind.Merge.Plugin.Handlers;
public interface IBlockCacheService
{
public ConcurrentDictionary<Hash256, Block> BlockCache { get; }
Hash256 FinalizedHash { get; set; }
Hash256? FinalizedHash { get; set; }
}
3 changes: 2 additions & 1 deletion src/Nethermind/Nethermind.Merge.Plugin/MergeGossipPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using Nethermind.Blockchain;
using Nethermind.Blockchain.Synchronization;
using Nethermind.Consensus;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Crypto;
Expand All @@ -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;

Expand All @@ -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();
}

Expand Down Expand Up @@ -105,6 +109,8 @@ lowestInsertedBeaconHeader is not null &&
/// <returns></returns>
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())
Expand All @@ -114,7 +120,7 @@ lowestInsertedBeaconHeader is not null &&
return null;
}

public Hash256 GetFinalizedHash()
public Hash256? GetFinalizedHash()
{
return _blockCacheService.FinalizedHash;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Optimism/OptimismPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,7 @@ public ScenarioBuilder WhenMergeSyncPivotNotResolvedYet()
{
SyncConfig.MaxAttemptsToUpdatePivot = 3;
BeaconSyncStrategy = Substitute.For<IBeaconSyncStrategy>();
BeaconSyncStrategy.GetFinalizedHash().Returns(TestItem.KeccakA);
BeaconSyncStrategy.MergeTransitionFinished.Returns(true);
return "merge sync pivot not resolved yet";
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -27,6 +28,8 @@ public interface IBeaconSyncStrategy
bool ShouldBeInBeaconModeControl();
bool IsBeaconSyncFinished(BlockHeader? blockHeader);

public bool MergeTransitionFinished { get; }

public long? GetTargetBlockHeight();
public Hash256? GetFinalizedHash();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down

0 comments on commit 74743cd

Please sign in to comment.