From a7d40b43225a7c6c050df79e0c7366b5752e05aa Mon Sep 17 00:00:00 2001 From: Shargon Date: Fri, 13 Dec 2019 19:04:24 +0100 Subject: [PATCH] Prevent Timer's lock with wrong dns (#1358) * 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 --- src/neo/Network/P2P/LocalNode.cs | 44 ++++++++++++++------------------ 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/src/neo/Network/P2P/LocalNode.cs b/src/neo/Network/P2P/LocalNode.cs index a651d46a16..680437e430 100644 --- a/src/neo/Network/P2P/LocalNode.cs +++ b/src/neo/Network/P2P/LocalNode.cs @@ -10,6 +10,7 @@ using System.Net.Sockets; using System.Reflection; using System.Threading; +using System.Threading.Tasks; namespace Neo.Network.P2P { @@ -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; @@ -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])); } } @@ -97,33 +104,18 @@ private static IPEndPoint GetIPEndpointFromHostPort(string hostNameOrAddress, in return new IPEndPoint(ipAddress, port); } - /// - /// Return an amount of random seeds nodes from the default SeedList file defined on . - /// - /// Limit of random seed nodes to be obtained, also limited by the available seeds from file. - private static IEnumerable 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 GetRemoteNodes() @@ -153,7 +145,9 @@ protected override void NeedMorePeers(int count) { // Will call AddPeers with default SeedList set cached on . // 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)); } }