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

Prevent Timer's lock with wrong dns #1358

Merged
merged 21 commits into from
Dec 13, 2019
Merged
Changes from 16 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
58 changes: 33 additions & 25 deletions src/neo/Network/P2P/LocalNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,24 @@
using System.Net.Sockets;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;

namespace Neo.Network.P2P
{
public class LocalNode : Peer
{
public class Seed
{
public string HostAndPort;
public IPEndPoint EndPoint;
}
public class Relay { public IInventory Inventory; }
internal class RelayDirectly { public IInventory Inventory; }
internal class SendDirectly { public IInventory Inventory; }

public const uint ProtocolVersion = 0;
private const int MaxCountFromSeedList = 5;
private readonly Seed[] SeedList;

private static readonly object lockObj = new object();
private readonly NeoSystem system;
Expand Down Expand Up @@ -56,6 +63,17 @@ public LocalNode(NeoSystem system)
throw new InvalidOperationException();
this.system = system;
singleton = this;

SeedList = ProtocolSettings.Default.SeedList
.Select(u => new Seed() { HostAndPort = u })
.ToArray();

// Start dns resolution in parallel

foreach (var seed in SeedList)
{
Task.Run(() => seed).PipeTo(Self, Sender);
}
}
}

Expand Down Expand Up @@ -97,33 +115,18 @@ private static IPEndPoint GetIPEndpointFromHostPort(string hostNameOrAddress, in
return new IPEndPoint(ipAddress, port);
}

/// <summary>
/// Return an amount of random seeds nodes from the default SeedList file defined on <see cref="ProtocolSettings"/>.
/// </summary>
/// <param name="seedsToTake">Limit of random seed nodes to be obtained, also limited by the available seeds from file.</param>
private static IEnumerable<IPEndPoint> GetIPEndPointsFromSeedList(int seedsToTake)
internal static IPEndPoint GetIpEndPoint(string hostAndPort)
{
if (seedsToTake > 0)
if (string.IsNullOrEmpty(hostAndPort)) return null;

try
{
Random rand = new Random();
foreach (string hostAndPort in ProtocolSettings.Default.SeedList.OrderBy(p => rand.Next()))
{
if (seedsToTake == 0) break;
string[] p = hostAndPort.Split(':');
IPEndPoint seed;
try
{
seed = GetIPEndpointFromHostPort(p[0], int.Parse(p[1]));
}
catch (AggregateException)
{
continue;
}
if (seed == null) continue;
seedsToTake--;
yield return seed;
}
string[] p = hostAndPort.Split(':');
return GetIPEndpointFromHostPort(p[0], int.Parse(p[1]));
}
catch { }

return null;
}

public IEnumerable<RemoteNode> GetRemoteNodes()
Expand Down Expand Up @@ -153,7 +156,9 @@ protected override void NeedMorePeers(int count)
{
// Will call AddPeers with default SeedList set cached on <see cref="ProtocolSettings"/>.
// It will try to add those, sequentially, to the list of currently uncconected ones.
AddPeers(GetIPEndPointsFromSeedList(count));

Random rand = new Random();
AddPeers(SeedList.Where(u => u.EndPoint != null).OrderBy(p => rand.Next()).Select(u => u.EndPoint).Take(count));
}
}

Expand All @@ -165,6 +170,9 @@ protected override void OnReceive(object message)
case Message msg:
BroadcastMessage(msg);
break;
case Seed seed:
if (seed.EndPoint == null) seed.EndPoint = GetIpEndPoint(seed.HostAndPort);
break;
case Relay relay:
OnRelay(relay.Inventory);
break;
Expand Down