Skip to content

Commit

Permalink
Prevent Timer's lock with wrong dns (#1358)
Browse files Browse the repository at this point in the history
* Prevent to lock Timer with wrong dns

* Parse dns en ProtocolSettings

* Update LocalNode.cs

* Update LocalNode.cs

* Update ProtocolSettings.cs

* Fix ut

* Update ProtocolSettings.cs

* Update UT_ProtocolSettings.cs

* dotnet format

* Process dns seeds in parallel

* Revert UT

* Revert protocol settings

* Update UT_ProtocolSettings.cs

* Update ProtocolSettings.cs

* Add comment

* Update LocalNode.cs

* Update LocalNode.cs

* Update LocalNode.cs

* Update LocalNode.cs

* Update LocalNode.cs
  • Loading branch information
shargon authored Dec 13, 2019
1 parent 64411d2 commit a7d40b4
Showing 1 changed file with 19 additions and 25 deletions.
44 changes: 19 additions & 25 deletions src/neo/Network/P2P/LocalNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Net.Sockets;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;

namespace Neo.Network.P2P
{
Expand All @@ -21,6 +22,7 @@ internal class SendDirectly { public IInventory Inventory; }

public const uint ProtocolVersion = 0;
private const int MaxCountFromSeedList = 5;
private readonly IPEndPoint[] SeedList = new IPEndPoint[ProtocolSettings.Default.SeedList.Length];

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

// Start dns resolution in parallel

for (int i = 0; i < ProtocolSettings.Default.SeedList.Length; i++)
Task.Run(() => SeedList[i] = GetIpEndPoint(ProtocolSettings.Default.SeedList[i]));
}
}

Expand Down Expand Up @@ -97,33 +104,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 +145,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 != null).OrderBy(p => rand.Next()).Take(count));
}
}

Expand Down

0 comments on commit a7d40b4

Please sign in to comment.