diff --git a/src/Nethermind/Nethermind.Synchronization.Test/SyncServerTests.cs b/src/Nethermind/Nethermind.Synchronization.Test/SyncServerTests.cs index 28a8b87709e..46cadf01253 100644 --- a/src/Nethermind/Nethermind.Synchronization.Test/SyncServerTests.cs +++ b/src/Nethermind/Nethermind.Synchronization.Test/SyncServerTests.cs @@ -155,6 +155,54 @@ public void Can_accept_blocks_that_are_fine() Assert.That(block.Header, Is.EqualTo(localBlockTree.BestSuggestedHeader)); } + [TestCase(SyncMode.SnapSync, false)] + [TestCase(SyncMode.FastSync, false)] + [TestCase(SyncMode.StateNodes, false)] + [TestCase(SyncMode.Full, true)] + public void Should_accept_or_not_blocks_depends_on_sync_mode(SyncMode syncMode, bool expectBlockAccepted) + { + Context ctx = new(); + BlockTree remoteBlockTree = Build.A.BlockTree().OfChainLength(10).TestObject; + BlockTree localBlockTree = Build.A.BlockTree().OfChainLength(9).TestObject; + + StaticSelector staticSelector; + switch (syncMode) + { + case SyncMode.SnapSync: + staticSelector = StaticSelector.SnapSync; + break; + case SyncMode.FastSync: + staticSelector = StaticSelector.FastSync; + break; + case SyncMode.StateNodes: + staticSelector = StaticSelector.StateNodesWithFastBlocks; + break; + default: + staticSelector = StaticSelector.Full; + break; + } + + ctx.SyncServer = new SyncServer( + new MemDb(), + new MemDb(), + localBlockTree, + NullReceiptStorage.Instance, + Always.Valid, + Always.Valid, + ctx.PeerPool, + staticSelector, + new SyncConfig(), + Policy.FullGossip, + MainnetSpecProvider.Instance, + LimboLogs.Instance); + + Block block = remoteBlockTree.FindBlock(9, BlockTreeLookupOptions.None)!; + + ctx.SyncServer.AddNewBlock(block, ctx.NodeWhoSentTheBlock); + + block.Header.Equals(localBlockTree.BestSuggestedHeader).Should().Be(expectBlockAccepted); + } + [Test] public void Terminal_block_with_lower_td_should_not_change_best_suggested_but_should_be_added_to_block_tree() { diff --git a/src/Nethermind/Nethermind.Synchronization/ParallelSync/StaticSelector.cs b/src/Nethermind/Nethermind.Synchronization/ParallelSync/StaticSelector.cs index e4304064a82..2bec837c611 100644 --- a/src/Nethermind/Nethermind.Synchronization/ParallelSync/StaticSelector.cs +++ b/src/Nethermind/Nethermind.Synchronization/ParallelSync/StaticSelector.cs @@ -14,6 +14,8 @@ public StaticSelector(SyncMode syncMode) public static StaticSelector Full { get; } = new(SyncMode.Full); + public static StaticSelector SnapSync { get; } = new(SyncMode.SnapSync); + public static StaticSelector FastSync { get; } = new(SyncMode.FastSync); public static StaticSelector FastBlocks { get; } = new(SyncMode.FastBlocks); diff --git a/src/Nethermind/Nethermind.Synchronization/SyncServer.cs b/src/Nethermind/Nethermind.Synchronization/SyncServer.cs index 30ed536806e..429f27af1be 100644 --- a/src/Nethermind/Nethermind.Synchronization/SyncServer.cs +++ b/src/Nethermind/Nethermind.Synchronization/SyncServer.cs @@ -198,9 +198,9 @@ public void AddNewBlock(Block block, ISyncPeer nodeWhoSentTheBlock) BroadcastBlock(blockToBroadCast, false, nodeWhoSentTheBlock); SyncMode syncMode = _syncModeSelector.Current; - bool notInFastSyncNorStateSync = (syncMode & (SyncMode.FastSync | SyncMode.StateNodes)) == SyncMode.None; + bool notInFastSyncNorStateSyncNorSnap = (syncMode & (SyncMode.FastSync | SyncMode.StateNodes | SyncMode.SnapSync)) == SyncMode.None; bool inFullSyncOrWaitingForBlocks = (syncMode & (SyncMode.Full | SyncMode.WaitingForBlock)) != SyncMode.None; - if (notInFastSyncNorStateSync || inFullSyncOrWaitingForBlocks) + if (notInFastSyncNorStateSyncNorSnap || inFullSyncOrWaitingForBlocks) { LogBlockAuthorNicely(block, nodeWhoSentTheBlock); SyncBlock(block, nodeWhoSentTheBlock);