Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If two peer have same speed, randomly try the other one. #5059

Merged
merged 2 commits into from
Dec 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using FluentAssertions;
using Nethermind.Blockchain.Synchronization;
using Nethermind.Core.Crypto;
using Nethermind.Core.Extensions;
using Nethermind.Core.Test.Builders;
using Nethermind.Crypto;
using Nethermind.Overseer.Test.JsonRpc.Dto;
using Nethermind.Stats;
using Nethermind.Stats.Model;
using Nethermind.Synchronization.Peers;
Expand Down Expand Up @@ -91,14 +87,31 @@ public void TestMinimumKnownSpeed(int peerWithKnownSpeed, int peerWithUnknownSpe
int selectedPeerIdx = peers.IndexOf(selectedPeer);
if (pickedNewPeer)
{
selectedPeerIdx.Should().Be(peerWithKnownSpeed);
selectedPeerIdx.Should().Be(peerWithKnownSpeed); // It picked the first peer with unknown speed
}
else
{
selectedPeerIdx.Should().Be(0);
selectedPeerIdx.Should().BeLessThan(peerWithKnownSpeed); // It picked earlier peers which have known speed
}
}

[Test]
public void TestWhenSameSpeed_RandomlyTryOtherPeer()
{
INodeStatsManager nodeStatsManager = Substitute.For<INodeStatsManager>();

List<PeerInfo> peers = Enumerable.Repeat<long?>(10, 50)
.Concat(Enumerable.Repeat<long?>(100, 50))
.Select((speed) => CreatePeerInfoWithSpeed(speed, nodeStatsManager))
.ToList();

BySpeedStrategy strategy = new(TransferSpeedType.Bodies, true, 0, 0, 0, 0);

PeerInfo? selectedPeer = strategy.Allocate(null, peers, nodeStatsManager, Build.A.BlockTree().TestObject);
int selectedPeerIdx = peers.IndexOf(selectedPeer);
selectedPeerIdx.Should().BeGreaterThan(50);
}

[TestCase(10, 0, 0, 0)]
[TestCase(10, 0, 1, 0)]
[TestCase(10, 10, 1, 0.5)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,15 @@ public BySpeedStrategy(
forceTake = true;
}

if (forceTake || (_priority ? averageTransferSpeed > bestPeer.TransferSpeed : averageTransferSpeed < bestPeer.TransferSpeed))
if (forceTake ||
(averageTransferSpeed == bestPeer.TransferSpeed ?

// If its the same speed, just randomly try it. Prevent getting stuck on the same peer on small network.
_random.NextSingle() < 0.25 :

(_priority ? averageTransferSpeed > bestPeer.TransferSpeed : averageTransferSpeed < bestPeer.TransferSpeed)
)
)
{
bestPeer = (info, averageTransferSpeed);
}
Expand Down