Skip to content

Commit

Permalink
Block beacon header sync until global pivot is updated (#7614)
Browse files Browse the repository at this point in the history
marcindsobczak authored Oct 22, 2024
1 parent 13f8286 commit 7588e22
Showing 5 changed files with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -90,6 +90,7 @@ protected IEngineRpcModule CreateEngineModule(MergeTestBlockchain chain, ISyncCo
chain.LogManager);
invalidChainTracker.SetupBlockchainProcessorInterceptor(chain.BlockchainProcessor);
chain.BeaconSync = new BeaconSync(chain.BeaconPivot, chain.BlockTree, synchronizationConfig, blockCacheService, chain.PoSSwitcher, chain.LogManager);
chain.BeaconSync.AllowBeaconHeaderSync();
EngineRpcCapabilitiesProvider capabilitiesProvider = new(chain.SpecProvider);
return new EngineRpcModule(
new GetPayloadV1Handler(
Original file line number Diff line number Diff line change
@@ -322,12 +322,12 @@ private ResultWrapper<ForkchoiceUpdatedV1Result> StartBuildingPayload(Block newH

private void StartNewBeaconHeaderSync(ForkchoiceStateV1 forkchoiceState, BlockHeader blockHeader, string requestStr)
{
_mergeSyncController.InitBeaconHeaderSync(blockHeader);
bool isSyncInitialized = _mergeSyncController.TryInitBeaconHeaderSync(blockHeader);
_beaconPivot.ProcessDestination = blockHeader;
_peerRefresher.RefreshPeers(blockHeader.Hash!, blockHeader.ParentHash!, forkchoiceState.FinalizedBlockHash);
_blockCacheService.FinalizedHash = forkchoiceState.FinalizedBlockHash;

if (_logger.IsInfo) _logger.Info($"Start a new sync process, Request: {requestStr}.");
if (isSyncInitialized && _logger.IsInfo) _logger.Info($"Start a new sync process, Request: {requestStr}.");
}

private bool IsInconsistent(Hash256 blockHash) => blockHash != Keccak.Zero && !_blockTree.IsMainChain(blockHash);
Original file line number Diff line number Diff line change
@@ -23,6 +23,10 @@ public class BeaconSync : IMergeSyncController, IBeaconSyncStrategy
private bool _isInBeaconModeControl = false;
private readonly ILogger _logger;

// beacon header sync can be initialized only when global pivot is already set,
// otherwise it might result in conflicting pivots and a deadlock
private bool _canInitBeaconHeaderSync = false;

public BeaconSync(
IBeaconPivot beaconPivot,
IBlockTree blockTree,
@@ -50,17 +54,25 @@ public void StopSyncing()
_isInBeaconModeControl = true;
}

public void InitBeaconHeaderSync(BlockHeader blockHeader)
public bool TryInitBeaconHeaderSync(BlockHeader blockHeader)
{
if (!_canInitBeaconHeaderSync) return false;

StopBeaconModeControl();
_beaconPivot.EnsurePivot(blockHeader);
return true;
}

public void StopBeaconModeControl()
{
_isInBeaconModeControl = false;
}

public void AllowBeaconHeaderSync()
{
_canInitBeaconHeaderSync = true;
}

public bool ShouldBeInBeaconHeaders()
{
bool beaconPivotExists = _beaconPivot.BeaconPivotExists();
@@ -130,7 +142,7 @@ public interface IMergeSyncController
{
void StopSyncing();

void InitBeaconHeaderSync(BlockHeader blockHeader);
bool TryInitBeaconHeaderSync(BlockHeader blockHeader);

void StopBeaconModeControl();
}
Original file line number Diff line number Diff line change
@@ -110,6 +110,7 @@ private async void OnSyncModeChanged(object? sender, SyncModeChangedEventArgs sy
{
_syncModeSelector.Changed -= OnSyncModeChanged;
_syncConfig.MaxAttemptsToUpdatePivot = 0;
_beaconSyncStrategy.AllowBeaconHeaderSync();
if (_logger.IsInfo) _logger.Info("Failed to update pivot block, skipping it and using pivot from config file.");
}
}
@@ -254,6 +255,7 @@ private void UpdateConfigValues(Hash256 finalizedBlockHash, long finalizedBlockN
_syncConfig.PivotHash = finalizedBlockHash.ToString();
_syncConfig.PivotNumber = finalizedBlockNumber.ToString();
_syncConfig.MaxAttemptsToUpdatePivot = 0;
_beaconSyncStrategy.AllowBeaconHeaderSync();
}

}
Original file line number Diff line number Diff line change
@@ -12,6 +12,8 @@ private No() { }

public static No BeaconSync { get; } = new();

public void AllowBeaconHeaderSync() { }

public bool ShouldBeInBeaconHeaders() => false;

public bool ShouldBeInBeaconModeControl() => false;
@@ -24,6 +26,7 @@ private No() { }

public interface IBeaconSyncStrategy
{
void AllowBeaconHeaderSync();
bool ShouldBeInBeaconHeaders();
bool ShouldBeInBeaconModeControl();
bool IsBeaconSyncFinished(BlockHeader? blockHeader);

0 comments on commit 7588e22

Please sign in to comment.