From da66d2b38abc392b547a2de6e1ec16ad82fdb60f Mon Sep 17 00:00:00 2001 From: Amirul Ashraf Date: Thu, 23 Feb 2023 14:11:36 +0800 Subject: [PATCH] Sleep peer if can't get account range --- .../AnalyzeResponsePerPeerTests.cs | 8 ++-- .../SnapSyncFeed/SnapSyncFeedTests.cs | 37 +++++++++++++++++++ .../SnapSync/SnapSyncFeed.cs | 7 +++- .../Synchronizer.cs | 2 +- 4 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 src/Nethermind/Nethermind.Synchronization.Test/SnapSync/SnapSyncFeed/SnapSyncFeedTests.cs diff --git a/src/Nethermind/Nethermind.Synchronization.Test/SnapSync/SnapSyncFeed/AnalyzeResponsePerPeerTests.cs b/src/Nethermind/Nethermind.Synchronization.Test/SnapSync/SnapSyncFeed/AnalyzeResponsePerPeerTests.cs index 6454a002b99..6838ba7c9e9 100644 --- a/src/Nethermind/Nethermind.Synchronization.Test/SnapSync/SnapSyncFeed/AnalyzeResponsePerPeerTests.cs +++ b/src/Nethermind/Nethermind.Synchronization.Test/SnapSync/SnapSyncFeed/AnalyzeResponsePerPeerTests.cs @@ -22,7 +22,7 @@ public void Test01() ISyncModeSelector selector = Substitute.For(); ISnapProvider snapProvider = Substitute.For(); - Synchronization.SnapSync.SnapSyncFeed feed = new(selector, snapProvider, null!, LimboLogs.Instance); + Synchronization.SnapSync.SnapSyncFeed feed = new(selector, snapProvider, LimboLogs.Instance); feed.AnalyzeResponsePerPeer(AddRangeResult.OK, peer1); feed.AnalyzeResponsePerPeer(AddRangeResult.OK, peer1); @@ -57,7 +57,7 @@ public void Test02() ISyncModeSelector selector = Substitute.For(); ISnapProvider snapProvider = Substitute.For(); - Synchronization.SnapSync.SnapSyncFeed feed = new(selector, snapProvider, null!, LimboLogs.Instance); + Synchronization.SnapSync.SnapSyncFeed feed = new(selector, snapProvider, LimboLogs.Instance); feed.AnalyzeResponsePerPeer(AddRangeResult.OK, peer1); feed.AnalyzeResponsePerPeer(AddRangeResult.OK, peer1); @@ -92,7 +92,7 @@ public void Test03() ISyncModeSelector selector = Substitute.For(); ISnapProvider snapProvider = Substitute.For(); - Synchronization.SnapSync.SnapSyncFeed feed = new(selector, snapProvider, null!, LimboLogs.Instance); + Synchronization.SnapSync.SnapSyncFeed feed = new(selector, snapProvider, LimboLogs.Instance); feed.AnalyzeResponsePerPeer(AddRangeResult.OK, peer2); feed.AnalyzeResponsePerPeer(AddRangeResult.OK, peer1); @@ -117,7 +117,7 @@ public void Test04() ISyncModeSelector selector = Substitute.For(); ISnapProvider snapProvider = Substitute.For(); - Synchronization.SnapSync.SnapSyncFeed feed = new(selector, snapProvider, null!, LimboLogs.Instance); + Synchronization.SnapSync.SnapSyncFeed feed = new(selector, snapProvider, LimboLogs.Instance); for (int i = 0; i < 200; i++) { diff --git a/src/Nethermind/Nethermind.Synchronization.Test/SnapSync/SnapSyncFeed/SnapSyncFeedTests.cs b/src/Nethermind/Nethermind.Synchronization.Test/SnapSync/SnapSyncFeed/SnapSyncFeedTests.cs new file mode 100644 index 00000000000..144af265e7c --- /dev/null +++ b/src/Nethermind/Nethermind.Synchronization.Test/SnapSync/SnapSyncFeed/SnapSyncFeedTests.cs @@ -0,0 +1,37 @@ +// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited +// SPDX-License-Identifier: LGPL-3.0-only + +using FluentAssertions; +using Nethermind.Blockchain.Synchronization; +using Nethermind.Core.Crypto; +using Nethermind.Logging; +using Nethermind.State.Snap; +using Nethermind.Synchronization.ParallelSync; +using Nethermind.Synchronization.Peers; +using Nethermind.Synchronization.SnapSync; +using NSubstitute; +using NUnit.Framework; + +namespace Nethermind.Synchronization.Test.SnapSync.SnapSyncFeed; + +public class SnapSyncFeedTests +{ + [Test] + public void WhenAccountRequestEmpty_ReturnNoProgress() + { + ISnapProvider snapProvider = Substitute.For(); + Synchronization.SnapSync.SnapSyncFeed feed = new( + Substitute.For(), snapProvider, LimboLogs.Instance); + + snapProvider.AddAccountRange(Arg.Any(), Arg.Any()) + .Returns(AddRangeResult.ExpiredRootHash); + + SnapSyncBatch response = new SnapSyncBatch(); + response.AccountRangeRequest = new AccountRange(Keccak.Zero, Keccak.Zero); + response.AccountRangeResponse = new AccountsAndProofs(); + + PeerInfo peer = new PeerInfo(Substitute.For()); + + feed.HandleResponse(response, peer).Should().Be(SyncResponseHandlingResult.NoProgress); + } +} diff --git a/src/Nethermind/Nethermind.Synchronization/SnapSync/SnapSyncFeed.cs b/src/Nethermind/Nethermind.Synchronization/SnapSync/SnapSyncFeed.cs index d9687fb2cc1..61bfd01abbe 100644 --- a/src/Nethermind/Nethermind.Synchronization/SnapSync/SnapSyncFeed.cs +++ b/src/Nethermind/Nethermind.Synchronization/SnapSync/SnapSyncFeed.cs @@ -33,7 +33,7 @@ public class SnapSyncFeed : SyncFeed, IDisposable public override bool IsMultiFeed => true; public override AllocationContexts Contexts => AllocationContexts.Snap; - public SnapSyncFeed(ISyncModeSelector syncModeSelector, ISnapProvider snapProvider, IBlockTree blockTree, ILogManager logManager) + public SnapSyncFeed(ISyncModeSelector syncModeSelector, ISnapProvider snapProvider, ILogManager logManager) { _syncModeSelector = syncModeSelector; _snapProvider = snapProvider; @@ -188,6 +188,11 @@ public SyncResponseHandlingResult AnalyzeResponsePerPeer(AddRangeResult result, } } + if (result == AddRangeResult.ExpiredRootHash) + { + return SyncResponseHandlingResult.NoProgress; + } + return SyncResponseHandlingResult.OK; } } diff --git a/src/Nethermind/Nethermind.Synchronization/Synchronizer.cs b/src/Nethermind/Nethermind.Synchronization/Synchronizer.cs index feb3ded17d0..964014a8c4a 100644 --- a/src/Nethermind/Nethermind.Synchronization/Synchronizer.cs +++ b/src/Nethermind/Nethermind.Synchronization/Synchronizer.cs @@ -153,7 +153,7 @@ private void StartStateSyncComponents() private void StartSnapSyncComponents() { - _snapSyncFeed = new SnapSyncFeed(_syncMode, _snapProvider, _blockTree, _logManager); + _snapSyncFeed = new SnapSyncFeed(_syncMode, _snapProvider, _logManager); SnapSyncDispatcher dispatcher = new(_snapSyncFeed!, _syncPeerPool, new SnapSyncAllocationStrategyFactory(), _logManager); Task _ = dispatcher.Start(_syncCancellation!.Token).ContinueWith(t =>