Skip to content

Commit

Permalink
fixing a few broken things around sync selector in the Full sync mode
Browse files Browse the repository at this point in the history
  • Loading branch information
tkstanczak committed Jun 4, 2019
1 parent ef86869 commit 182dd49
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ namespace Nethermind.Blockchain.Test.Synchronization
public class SyncModeSelectorTests
{
[Test]
public void Starts_with_headers_when_fast_sync_is_enabled()
public void Starts_with_not_started_in_fast_sync_enabled()
{
SyncModeSelector selector = BuildSelector(true);
Assert.AreEqual(SyncMode.FastBlocks, selector.Current);
Assert.AreEqual(SyncMode.NotStarted, selector.Current);
}

[Test]
public void Starts_with_full_when_fast_sync_is_disabled()
public void Starts_with_not_started()
{
SyncModeSelector selector = BuildSelector(false);
Assert.AreEqual(SyncMode.Full, selector.Current);
Assert.AreEqual(SyncMode.NotStarted, selector.Current);
}

[Test]
Expand All @@ -62,7 +62,7 @@ public void Can_keep_changing_in_fast_sync()
ISyncProgressResolver syncProgressResolver = Substitute.For<ISyncProgressResolver>();

SyncModeSelector selector = new SyncModeSelector(syncProgressResolver, syncPeerPool, syncConfig, LimboLogs.Instance);
Assert.AreEqual(SyncMode.FastBlocks, selector.Current);
Assert.AreEqual(SyncMode.NotStarted, selector.Current);

(long BestRemote, long BestLocalHeader, long BestLocalFullBlock, long BestLocalState, SyncMode ExpectedState, string Description)[] states =
{
Expand Down Expand Up @@ -120,12 +120,12 @@ public void Selects_correctly(bool useFastSync, long bestRemote, long bestHeader
}
}

[TestCase(true, 1032, 0, 0, 0, SyncMode.FastBlocks)]
[TestCase(false, 1032, 0, 0, 0, SyncMode.Full)]
[TestCase(true, 1032, 1000, 0, 0, SyncMode.FastBlocks)]
[TestCase(false, 1032, 1000, 0, 0, SyncMode.Full)]
[TestCase(true, 1032, 1000, 0, 1000, SyncMode.FastBlocks)]
[TestCase(false, 1032, 1000, 0, 1000, SyncMode.Full)]
[TestCase(true, 1032, 0, 0, 0, SyncMode.NotStarted)]
[TestCase(false, 1032, 0, 0, 0, SyncMode.NotStarted)]
[TestCase(true, 1032, 1000, 0, 0, SyncMode.NotStarted)]
[TestCase(false, 1032, 1000, 0, 0, SyncMode.NotStarted)]
[TestCase(true, 1032, 1000, 0, 1000, SyncMode.NotStarted)]
[TestCase(false, 1032, 1000, 0, 1000, SyncMode.NotStarted)]
public void Does_not_change_when_no_peers(bool useFastSync, long bestRemote, long bestLocalHeader, long bestLocalFullBLock, long bestLocalState, SyncMode expected)
{
SyncModeSelector selector = BuildSelectorNoPeers(useFastSync, bestRemote, bestLocalHeader, bestLocalFullBLock, bestLocalState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,54 +54,65 @@ public void Update()
{
return;
}

long bestHeader = _syncProgressResolver.FindBestHeader();
long bestFullBlock = _syncProgressResolver.FindBestFullBlock();
long bestFullState = _syncProgressResolver.FindBestFullState();
long maxBlockNumberAmongPeers = 0;
if (bestFullBlock < 0
|| bestHeader < 0
|| bestFullState < 0
|| bestFullBlock > bestHeader)
{
string errorMessage = $"Invalid best state calculation: {BuildStateString(bestHeader, bestFullBlock, bestFullBlock, maxBlockNumberAmongPeers)}";
if(_logger.IsError) _logger.Error(errorMessage);
throw new InvalidOperationException(errorMessage);
}

foreach (PeerInfo peerInfo in _syncPeerPool.UsefulPeers)
{
maxBlockNumberAmongPeers = Math.Max(maxBlockNumberAmongPeers, peerInfo.HeadNumber);
}

SyncMode newSyncMode;
if (!_syncProgressResolver.IsFastBlocksFinished())
{
newSyncMode = SyncMode.FastBlocks;
}
else if (maxBlockNumberAmongPeers - Math.Max(bestFullState, bestFullBlock) <= FullSyncThreshold)
if (!_syncConfig.FastSync)
{
newSyncMode = Math.Max(bestFullState, bestFullBlock) >= bestHeader ? SyncMode.Full : SyncMode.StateNodes;
}
else if (maxBlockNumberAmongPeers - bestHeader <= FullSyncThreshold)
{
// TODO: we need to check here if there are any blocks in processing queue... any other checks are wrong
newSyncMode = bestFullBlock > bestFullState ? SyncMode.WaitForProcessor : SyncMode.StateNodes;
if (Current == SyncMode.NotStarted)
{
Current = SyncMode.Full;
Changed?.Invoke(this, EventArgs.Empty);
}
}
else
{
newSyncMode = bestFullBlock > bestFullState ? SyncMode.WaitForProcessor : SyncMode.Headers;
}
long bestHeader = _syncProgressResolver.FindBestHeader();
long bestFullBlock = _syncProgressResolver.FindBestFullBlock();
long bestFullState = _syncProgressResolver.FindBestFullState();
long maxBlockNumberAmongPeers = 0;
if (bestFullBlock < 0
|| bestHeader < 0
|| bestFullState < 0
|| bestFullBlock > bestHeader)
{
string errorMessage = $"Invalid best state calculation: {BuildStateString(bestHeader, bestFullBlock, bestFullBlock, maxBlockNumberAmongPeers)}";
if (_logger.IsError) _logger.Error(errorMessage);
throw new InvalidOperationException(errorMessage);
}

if (newSyncMode != Current)
{
if (_logger.IsInfo) _logger.Info($"Switching sync mode from {Current} to {newSyncMode} {BuildStateString(bestHeader, bestFullBlock, bestFullState, maxBlockNumberAmongPeers)}");
Current = newSyncMode;
Changed?.Invoke(this, EventArgs.Empty);
}
else
{
if (_logger.IsInfo) _logger.Info($"Staying on sync mode {Current} {BuildStateString(bestHeader, bestFullBlock, bestFullState, maxBlockNumberAmongPeers)}");
foreach (PeerInfo peerInfo in _syncPeerPool.UsefulPeers)
{
maxBlockNumberAmongPeers = Math.Max(maxBlockNumberAmongPeers, peerInfo.HeadNumber);
}

SyncMode newSyncMode;
if (!_syncProgressResolver.IsFastBlocksFinished())
{
newSyncMode = SyncMode.FastBlocks;
}
else if (maxBlockNumberAmongPeers - Math.Max(bestFullState, bestFullBlock) <= FullSyncThreshold)
{
newSyncMode = Math.Max(bestFullState, bestFullBlock) >= bestHeader ? SyncMode.Full : SyncMode.StateNodes;
}
else if (maxBlockNumberAmongPeers - bestHeader <= FullSyncThreshold)
{
// TODO: we need to check here if there are any blocks in processing queue... any other checks are wrong
newSyncMode = bestFullBlock > bestFullState ? SyncMode.WaitForProcessor : SyncMode.StateNodes;
}
else
{
newSyncMode = bestFullBlock > bestFullState ? SyncMode.WaitForProcessor : SyncMode.Headers;
}

if (newSyncMode != Current)
{
if (_logger.IsInfo) _logger.Info($"Switching sync mode from {Current} to {newSyncMode} {BuildStateString(bestHeader, bestFullBlock, bestFullState, maxBlockNumberAmongPeers)}");
Current = newSyncMode;
Changed?.Invoke(this, EventArgs.Empty);
}
else
{
if (_logger.IsInfo) _logger.Info($"Staying on sync mode {Current} {BuildStateString(bestHeader, bestFullBlock, bestFullState, maxBlockNumberAmongPeers)}");
}
}
}

Expand Down

0 comments on commit 182dd49

Please sign in to comment.