Skip to content

Commit

Permalink
Allow specifying local ip. And use any by default instead of loopback. (
Browse files Browse the repository at this point in the history
#5635)

* Allow specifying local ip. And use any by default instead of loopback.

* Fix does not even start

* Whitespace

* Minor cleanup
  • Loading branch information
asdacap authored May 3, 2023
1 parent f6b1fa2 commit 1c03616
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 44 deletions.
1 change: 1 addition & 0 deletions src/Nethermind/Nethermind.Init/Steps/InitializeNetwork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ private async Task InitPeer()
_api.MessageSerializationService,
_api.NodeKey.PublicKey,
_networkConfig.P2PPort,
_networkConfig.LocalIp,
encryptionHandshakeServiceA,
_api.SessionMonitor,
_api.DisconnectsAnalyzer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public async Task Start_stop()
{
RlpxHost host = new(
Substitute.For<IMessageSerializationService>(),
TestItem.PublicKeyA, GegAvailableLocalPort(),
TestItem.PublicKeyA, GegAvailableLocalPort(), null,
Substitute.For<IHandshakeService>(),
Substitute.For<ISessionMonitor>(),
NullDisconnectsAnalyzer.Instance,
Expand Down
39 changes: 0 additions & 39 deletions src/Nethermind/Nethermind.Network/IP/SocketIPSource.cs

This file was deleted.

5 changes: 2 additions & 3 deletions src/Nethermind/Nethermind.Network/IPResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,14 @@ IEnumerable<IIPSource> GetIPSources()
if (_logger.IsError) _logger.Error("Error while getting external ip", e);
}

return IPAddress.Loopback;
return IPAddress.Any;
}

private async Task<IPAddress> InitializeLocalIp()
{
IEnumerable<IIPSource> GetIPSources()
{
yield return new NetworkConfigLocalIPSource(_networkConfig, _logManager);
yield return new SocketIPSource(_logManager);
}

try
Expand All @@ -107,7 +106,7 @@ IEnumerable<IIPSource> GetIPSources()
if (_logger.IsError) _logger.Error("Error while getting local ip", e);
}

return IPAddress.Loopback;
return IPAddress.Any;
}
}
}
9 changes: 8 additions & 1 deletion src/Nethermind/Nethermind.Network/Rlpx/RlpxHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class RlpxHost : IRlpxHost
private bool _isInitialized;
public PublicKey LocalNodeId { get; }
public int LocalPort { get; }
public string? LocalIp { get; set; }
private readonly IHandshakeService _handshakeService;
private readonly IMessageSerializationService _serializationService;
private readonly ILogManager _logManager;
Expand All @@ -47,6 +48,7 @@ public class RlpxHost : IRlpxHost
public RlpxHost(IMessageSerializationService serializationService,
PublicKey localNodeId,
int localPort,
string? localIp,
IHandshakeService handshakeService,
ISessionMonitor sessionMonitor,
IDisconnectsAnalyzer disconnectsAnalyzer,
Expand Down Expand Up @@ -78,6 +80,7 @@ TimeSpan sendLatency
_handshakeService = handshakeService ?? throw new ArgumentNullException(nameof(handshakeService));
LocalNodeId = localNodeId ?? throw new ArgumentNullException(nameof(localNodeId));
LocalPort = localPort;
LocalIp = localIp;
_sendLatency = sendLatency;
}

Expand Down Expand Up @@ -109,7 +112,11 @@ public async Task Init()
InitializeChannel(ch, session);
}));

_bootstrapChannel = await bootstrap.BindAsync(LocalPort).ContinueWith(t =>
Task<IChannel> openTask = LocalIp is null
? bootstrap.BindAsync(LocalPort)
: bootstrap.BindAsync(IPAddress.Parse(LocalIp), LocalPort);

_bootstrapChannel = await openTask.ContinueWith(t =>
{
if (t.IsFaulted)
{
Expand Down

0 comments on commit 1c03616

Please sign in to comment.