diff --git a/src/Nethermind/Nethermind.Blockchain.Test/Synchronization/SyncModeSelectorTests.cs b/src/Nethermind/Nethermind.Blockchain.Test/Synchronization/SyncModeSelectorTests.cs index 4f9b2ec371a..0a0d9837246 100644 --- a/src/Nethermind/Nethermind.Blockchain.Test/Synchronization/SyncModeSelectorTests.cs +++ b/src/Nethermind/Nethermind.Blockchain.Test/Synchronization/SyncModeSelectorTests.cs @@ -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] @@ -62,7 +62,7 @@ public void Can_keep_changing_in_fast_sync() ISyncProgressResolver syncProgressResolver = Substitute.For(); 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 = { @@ -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); diff --git a/src/Nethermind/Nethermind.Blockchain/Synchronization/SyncModeSelector.cs b/src/Nethermind/Nethermind.Blockchain/Synchronization/SyncModeSelector.cs index fa51ac0a8af..e989d62c8ea 100644 --- a/src/Nethermind/Nethermind.Blockchain/Synchronization/SyncModeSelector.cs +++ b/src/Nethermind/Nethermind.Blockchain/Synchronization/SyncModeSelector.cs @@ -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)}"); + } } }