1010using System . Net . Sockets ;
1111using System . Reflection ;
1212using System . Threading ;
13+ using System . Threading . Tasks ;
1314
1415namespace Neo . Network . P2P
1516{
@@ -21,6 +22,7 @@ internal class SendDirectly { public IInventory Inventory; }
2122
2223 public const uint ProtocolVersion = 0 ;
2324 private const int MaxCountFromSeedList = 5 ;
25+ private readonly IPEndPoint [ ] SeedList = new IPEndPoint [ ProtocolSettings . Default . SeedList . Length ] ;
2426
2527 private static readonly object lockObj = new object ( ) ;
2628 private readonly NeoSystem system ;
@@ -56,6 +58,14 @@ public LocalNode(NeoSystem system)
5658 throw new InvalidOperationException ( ) ;
5759 this . system = system ;
5860 singleton = this ;
61+
62+ // Start dns resolution in parallel
63+
64+ for ( int i = 0 ; i < ProtocolSettings . Default . SeedList . Length ; i ++ )
65+ {
66+ int index = i ;
67+ Task . Run ( ( ) => SeedList [ index ] = GetIpEndPoint ( ProtocolSettings . Default . SeedList [ index ] ) ) ;
68+ }
5969 }
6070 }
6171
@@ -97,33 +107,18 @@ private static IPEndPoint GetIPEndpointFromHostPort(string hostNameOrAddress, in
97107 return new IPEndPoint ( ipAddress , port ) ;
98108 }
99109
100- /// <summary>
101- /// Return an amount of random seeds nodes from the default SeedList file defined on <see cref="ProtocolSettings"/>.
102- /// </summary>
103- /// <param name="seedsToTake">Limit of random seed nodes to be obtained, also limited by the available seeds from file.</param>
104- private static IEnumerable < IPEndPoint > GetIPEndPointsFromSeedList ( int seedsToTake )
110+ internal static IPEndPoint GetIpEndPoint ( string hostAndPort )
105111 {
106- if ( seedsToTake > 0 )
112+ if ( string . IsNullOrEmpty ( hostAndPort ) ) return null ;
113+
114+ try
107115 {
108- Random rand = new Random ( ) ;
109- foreach ( string hostAndPort in ProtocolSettings . Default . SeedList . OrderBy ( p => rand . Next ( ) ) )
110- {
111- if ( seedsToTake == 0 ) break ;
112- string [ ] p = hostAndPort . Split ( ':' ) ;
113- IPEndPoint seed ;
114- try
115- {
116- seed = GetIPEndpointFromHostPort ( p [ 0 ] , int . Parse ( p [ 1 ] ) ) ;
117- }
118- catch ( AggregateException )
119- {
120- continue ;
121- }
122- if ( seed == null ) continue ;
123- seedsToTake -- ;
124- yield return seed ;
125- }
116+ string [ ] p = hostAndPort . Split ( ':' ) ;
117+ return GetIPEndpointFromHostPort ( p [ 0 ] , int . Parse ( p [ 1 ] ) ) ;
126118 }
119+ catch { }
120+
121+ return null ;
127122 }
128123
129124 public IEnumerable < RemoteNode > GetRemoteNodes ( )
@@ -153,7 +148,9 @@ protected override void NeedMorePeers(int count)
153148 {
154149 // Will call AddPeers with default SeedList set cached on <see cref="ProtocolSettings"/>.
155150 // It will try to add those, sequentially, to the list of currently uncconected ones.
156- AddPeers ( GetIPEndPointsFromSeedList ( count ) ) ;
151+
152+ Random rand = new Random ( ) ;
153+ AddPeers ( SeedList . Where ( u => u != null ) . OrderBy ( p => rand . Next ( ) ) . Take ( count ) ) ;
157154 }
158155 }
159156
0 commit comments