diff --git a/src/Nethermind/Nethermind.Network.Test/Discovery/DiscoveryManagerTests.cs b/src/Nethermind/Nethermind.Network.Test/Discovery/DiscoveryManagerTests.cs index 2983bf8ec66..92e1b9195b7 100644 --- a/src/Nethermind/Nethermind.Network.Test/Discovery/DiscoveryManagerTests.cs +++ b/src/Nethermind/Nethermind.Network.Test/Discovery/DiscoveryManagerTests.cs @@ -50,6 +50,7 @@ public class DiscoveryManagerTests private string _host = "192.168.1.17"; private Node[] _nodes; private PublicKey _publicKey; + private IIPResolver _ipResolver; [SetUp] public void Initialize() @@ -75,13 +76,15 @@ public void Initialize() _timestamper = Timestamper.Default; + _ipResolver = new IPResolver(_networkConfig, logManager); + var evictionManager = new EvictionManager(_nodeTable, logManager); var lifecycleFactory = new NodeLifecycleManagerFactory(_nodeTable, new DiscoveryMessageFactory(_timestamper), evictionManager, new NodeStatsManager(statsConfig, logManager), discoveryConfig, logManager); _nodes = new[] {new Node("192.168.1.18", 1), new Node("192.168.1.19", 2)}; IFullDb nodeDb = new SimpleFilePublicKeyDb("Test", "test_db", logManager); - _discoveryManager = new DiscoveryManager(lifecycleFactory, _nodeTable, new NetworkStorage(nodeDb, logManager), discoveryConfig, logManager); + _discoveryManager = new DiscoveryManager(lifecycleFactory, _nodeTable, new NetworkStorage(nodeDb, logManager), discoveryConfig, logManager, _ipResolver); _discoveryManager.MessageSender = _messageSender; } diff --git a/src/Nethermind/Nethermind.Network.Test/Discovery/NettyDiscoveryHandlerTests.cs b/src/Nethermind/Nethermind.Network.Test/Discovery/NettyDiscoveryHandlerTests.cs index a2b4bc49c3a..f1f1dd3d272 100644 --- a/src/Nethermind/Nethermind.Network.Test/Discovery/NettyDiscoveryHandlerTests.cs +++ b/src/Nethermind/Nethermind.Network.Test/Discovery/NettyDiscoveryHandlerTests.cs @@ -47,7 +47,7 @@ public class NettyDiscoveryHandlerTests private readonly PrivateKey _privateKey2 = new PrivateKey("3a1076bf45ab87712ad64ccb3b10217737f7faacbf2872e88fdd9a537d8fe266"); private List _channels; private List _discoveryHandlers; - private List _discoveryManagers; + private List _discoveryManagersMocks; private readonly IPEndPoint _address = new IPEndPoint(IPAddress.Loopback, 10001); private readonly IPEndPoint _address2 = new IPEndPoint(IPAddress.Loopback, 10002); private int _channelActivatedCounter; @@ -57,21 +57,19 @@ public async Task Initialize() { _channels = new List(); _discoveryHandlers = new List(); - _discoveryManagers = new List(); + _discoveryManagersMocks = new List(); _channelActivatedCounter = 0; - var discoveryManager = Substitute.For(); + var discoveryManagerMock = Substitute.For(); var messageSerializationService = Build.A.SerializationService().WithDiscovery(_privateKey).TestObject; - var discoveryManager2 = Substitute.For(); + var discoveryManagerMock2 = Substitute.For(); var messageSerializationService2 = Build.A.SerializationService().WithDiscovery(_privateKey).TestObject; - await StartUdpChannel("127.0.0.1", 10001, discoveryManager, messageSerializationService); - await StartUdpChannel("127.0.0.1", 10002, discoveryManager2, messageSerializationService2); + await StartUdpChannel("127.0.0.1", 10001, discoveryManagerMock, messageSerializationService); + await StartUdpChannel("127.0.0.1", 10002, discoveryManagerMock2, messageSerializationService2); - _discoveryManagers.Add(discoveryManager); - _discoveryManagers.Add(discoveryManager2); - - Thread.Sleep(50); + _discoveryManagersMocks.Add(discoveryManagerMock); + _discoveryManagersMocks.Add(discoveryManagerMock2); Assert.AreEqual(2, _channelActivatedCounter); } @@ -97,7 +95,7 @@ public void PingSentReceivedTest() }; _discoveryHandlers[0].SendMessage(msg); SleepWhileWaiting(); - _discoveryManagers[1].Received(1).OnIncomingMessage(Arg.Is(x => x.MessageType == MessageType.Ping)); + _discoveryManagersMocks[1].Received(1).OnIncomingMessage(Arg.Is(x => x.MessageType == MessageType.Ping)); var msg2 = new PingMessage { @@ -109,7 +107,7 @@ public void PingSentReceivedTest() }; _discoveryHandlers[1].SendMessage(msg2); SleepWhileWaiting(); - _discoveryManagers[0].Received(1).OnIncomingMessage(Arg.Is(x => x.MessageType == MessageType.Ping)); + _discoveryManagersMocks[0].Received(1).OnIncomingMessage(Arg.Is(x => x.MessageType == MessageType.Ping)); } [Test] @@ -125,7 +123,7 @@ public void PongSentReceivedTest() }; _discoveryHandlers[0].SendMessage(msg); SleepWhileWaiting(); - _discoveryManagers[1].Received(1).OnIncomingMessage(Arg.Is(x => x.MessageType == MessageType.Pong)); + _discoveryManagersMocks[1].Received(1).OnIncomingMessage(Arg.Is(x => x.MessageType == MessageType.Pong)); var msg2 = new PongMessage { @@ -136,14 +134,9 @@ public void PongSentReceivedTest() }; _discoveryHandlers[1].SendMessage(msg2); SleepWhileWaiting(); - _discoveryManagers[0].Received(1).OnIncomingMessage(Arg.Is(x => x.MessageType == MessageType.Pong)); - } - - private static void SleepWhileWaiting() - { - Thread.Sleep((TestContext.CurrentContext.CurrentRepeatCount + 1) * 300); + _discoveryManagersMocks[0].Received(1).OnIncomingMessage(Arg.Is(x => x.MessageType == MessageType.Pong)); } - + [Test] [Retry(5)] public void FindNodeSentReceivedTest() @@ -157,7 +150,7 @@ public void FindNodeSentReceivedTest() }; _discoveryHandlers[0].SendMessage(msg); SleepWhileWaiting(); - _discoveryManagers[1].Received(1).OnIncomingMessage(Arg.Is(x => x.MessageType == MessageType.FindNode)); + _discoveryManagersMocks[1].Received(1).OnIncomingMessage(Arg.Is(x => x.MessageType == MessageType.FindNode)); var msg2 = new FindNodeMessage { @@ -168,7 +161,7 @@ public void FindNodeSentReceivedTest() }; _discoveryHandlers[1].SendMessage(msg2); SleepWhileWaiting(); - _discoveryManagers[0].Received(1).OnIncomingMessage(Arg.Is(x => x.MessageType == MessageType.FindNode)); + _discoveryManagersMocks[0].Received(1).OnIncomingMessage(Arg.Is(x => x.MessageType == MessageType.FindNode)); } [Test] @@ -184,7 +177,7 @@ public void NeighborsSentReceivedTest() }; _discoveryHandlers[0].SendMessage(msg); SleepWhileWaiting(); - _discoveryManagers[1].Received(1).OnIncomingMessage(Arg.Is(x => x.MessageType == MessageType.Neighbors)); + _discoveryManagersMocks[1].Received(1).OnIncomingMessage(Arg.Is(x => x.MessageType == MessageType.Neighbors)); var msg2 = new NeighborsMessage { @@ -195,7 +188,7 @@ public void NeighborsSentReceivedTest() }; _discoveryHandlers[1].SendMessage(msg2); SleepWhileWaiting(); - _discoveryManagers[0].Received(1).OnIncomingMessage(Arg.Is(x => x.MessageType == MessageType.Neighbors)); + _discoveryManagersMocks[0].Received(1).OnIncomingMessage(Arg.Is(x => x.MessageType == MessageType.Neighbors)); } private async Task StartUdpChannel(string address, int port, IDiscoveryManager discoveryManager, IMessageSerializationService service) @@ -224,5 +217,10 @@ private void InitializeChannel(IDatagramChannel channel, IDiscoveryManager disco .AddLast(new LoggingHandler(DotNetty.Handlers.Logging.LogLevel.TRACE)) .AddLast(handler); } + + private static void SleepWhileWaiting() + { + Thread.Sleep((TestContext.CurrentContext.CurrentRepeatCount + 1) * 300); + } } } \ No newline at end of file diff --git a/src/Nethermind/Nethermind.Network.Test/Discovery/NodeLifecycleManagerTests.cs b/src/Nethermind/Nethermind.Network.Test/Discovery/NodeLifecycleManagerTests.cs index 4fdf798ac45..a3db6b44d29 100644 --- a/src/Nethermind/Nethermind.Network.Test/Discovery/NodeLifecycleManagerTests.cs +++ b/src/Nethermind/Nethermind.Network.Test/Discovery/NodeLifecycleManagerTests.cs @@ -48,6 +48,7 @@ public class NodeLifecycleManagerTests private INodeTable _nodeTable; private IConfigProvider _configurationProvider; private ITimestamper _timestamper; + private IIPResolver _ipResolverMock; private int _port = 1; private string _host = "192.168.1.27"; @@ -69,6 +70,8 @@ public void Initialize() discoveryConfig.BucketSize = 3; discoveryConfig.BucketsCount = 1; + _ipResolverMock = Substitute.For(); + IStatsConfig statsConfig = _configurationProvider.GetConfig(); var calculator = new NodeDistanceCalculator(discoveryConfig); @@ -84,7 +87,7 @@ public void Initialize() _udpClient = Substitute.For(); var discoveryDb = new SimpleFilePublicKeyDb("Test","test", logManager); - _discoveryManager = new DiscoveryManager(lifecycleFactory, _nodeTable, new NetworkStorage(discoveryDb, logManager), discoveryConfig, logManager); + _discoveryManager = new DiscoveryManager(lifecycleFactory, _nodeTable, new NetworkStorage(discoveryDb, logManager), discoveryConfig, logManager, _ipResolverMock); _discoveryManager.MessageSender = _udpClient; } diff --git a/src/Nethermind/Nethermind.Network/Discovery/DiscoveryManager.cs b/src/Nethermind/Nethermind.Network/Discovery/DiscoveryManager.cs index ab0d06971d4..b97af663cc1 100644 --- a/src/Nethermind/Nethermind.Network/Discovery/DiscoveryManager.cs +++ b/src/Nethermind/Nethermind.Network/Discovery/DiscoveryManager.cs @@ -43,6 +43,7 @@ public class DiscoveryManager : IDiscoveryManager private readonly ConcurrentDictionary _nodeLifecycleManagers = new ConcurrentDictionary(); private readonly INodeTable _nodeTable; private readonly INetworkStorage _discoveryStorage; + private readonly IIPResolver _ipResolver; private readonly ConcurrentDictionary> _waitingEvents = new ConcurrentDictionary>(); private IMessageSender _messageSender; @@ -52,7 +53,8 @@ public DiscoveryManager( INodeTable nodeTable, INetworkStorage discoveryStorage, IDiscoveryConfig discoveryConfig, - ILogManager logManager) + ILogManager logManager, + IIPResolver ipResolver) { _logger = logManager.GetClassLogger() ?? throw new ArgumentNullException(nameof(logManager)); _discoveryConfig = discoveryConfig ?? throw new ArgumentNullException(nameof(discoveryConfig)); @@ -60,6 +62,7 @@ public DiscoveryManager( _nodeTable = nodeTable ?? throw new ArgumentNullException(nameof(nodeTable)); _discoveryStorage = discoveryStorage ?? throw new ArgumentNullException(nameof(discoveryStorage)); _nodeLifecycleManagerFactory.DiscoveryManager = this; + _ipResolver = ipResolver; } public IMessageSender MessageSender @@ -72,7 +75,6 @@ public void OnIncomingMessage(DiscoveryMessage message) try { if (_logger.IsTrace) _logger.Trace($"Received msg: {message}"); - MessageType msgType = message.MessageType; Node node = new Node(message.FarPublicKey, message.FarAddress); @@ -155,7 +157,6 @@ public INodeLifecycleManager GetNodeLifecycleManager(Node node, bool isPersisted public void SendMessage(DiscoveryMessage discoveryMessage) { if (_logger.IsTrace) _logger.Trace($"Sending msg: {discoveryMessage}"); - try { if (discoveryMessage is PingMessage pingMessage) @@ -208,17 +209,11 @@ public IReadOnlyCollection GetOrAddNodeLifecycleManagers( private bool ValidatePingAddress(PingMessage message) { - if (message.DestinationAddress == null || message.SourceAddress == null || message.FarAddress == null) + if (message.DestinationAddress == null || message.FarAddress == null) { if (_logger.IsDebug) _logger.Debug($"Received a ping message with empty address, message: {message}"); return false; } - - if (!Bytes.AreEqual(_nodeTable.MasterNode.Address.Address.MapToIPv6().GetAddressBytes(), message.DestinationAddress?.Address.MapToIPv6().GetAddressBytes())) - { - if (_logger.IsDebug) _logger.Debug($"Received a message with incorrect destination address, message: {message}"); - return false; - } #region // port will be different as we dynamically open ports for each socket connection diff --git a/src/Nethermind/Nethermind.Network/Discovery/Lifecycle/NodeLifecycleManager.cs b/src/Nethermind/Nethermind.Network/Discovery/Lifecycle/NodeLifecycleManager.cs index 2e1ab572d1b..59527c51d48 100644 --- a/src/Nethermind/Nethermind.Network/Discovery/Lifecycle/NodeLifecycleManager.cs +++ b/src/Nethermind/Nethermind.Network/Discovery/Lifecycle/NodeLifecycleManager.cs @@ -60,7 +60,7 @@ public NodeLifecycleManager(Node node, IDiscoveryManager discoveryManager, INode public Node ManagedNode { get; } public NodeLifecycleState State { get; private set; } public INodeStats NodeStats { get; } - public bool IsBonded => (_sentPing && _receivedPong) || (_receivedPing && _sentPong); + public bool IsBonded => (_sentPing && _receivedPong) && (_receivedPing && _sentPong); public event EventHandler OnStateChanged; @@ -95,6 +95,8 @@ public void ProcessPongMessage(PongMessage discoveryMessage) else { // ignore spoofed message + _receivedPong = false; + return; } } @@ -179,6 +181,7 @@ public void SendPong(PingMessage discoveryMessage) { PongMessage msg = _discoveryMessageFactory.CreateOutgoingMessage(ManagedNode); msg.PingMdc = discoveryMessage.Mdc; + _discoveryManager.SendMessage(msg); NodeStats.AddNodeStatsEvent(NodeStatsEventType.DiscoveryPongOut); _sentPong = true; @@ -192,7 +195,8 @@ public void SendNeighbors(Node[] nodes) { if (!IsBonded) { - if (_logger.IsWarn) _logger.Warn("Sending NEIGHBOURS before bonding"); + if (_logger.IsWarn) _logger.Warn("Attempt to send NEIGHBOURS before bonding"); + return; } NeighborsMessage msg = _discoveryMessageFactory.CreateOutgoingMessage(ManagedNode); diff --git a/src/Nethermind/Nethermind.Network/Discovery/NettyDiscoveryHandler.cs b/src/Nethermind/Nethermind.Network/Discovery/NettyDiscoveryHandler.cs index 7e70834b19a..853fbd20695 100644 --- a/src/Nethermind/Nethermind.Network/Discovery/NettyDiscoveryHandler.cs +++ b/src/Nethermind/Nethermind.Network/Discovery/NettyDiscoveryHandler.cs @@ -94,7 +94,6 @@ await _channel.WriteAndFlushAsync(packet).ContinueWith(t => } }); } - protected override void ChannelRead0(IChannelHandlerContext ctx, DatagramPacket packet) { IByteBuffer content = packet.Content; @@ -197,19 +196,19 @@ private bool ValidateMessage(DiscoveryMessage message, MessageType type, EndPoin return true; } - - private static void ReportMessageByType(DiscoveryMessage message) + + private void ReportMessageByType(DiscoveryMessage message) { - if (message is PingMessage pingMessage) - { - if (NetworkDiagTracer.IsEnabled) NetworkDiagTracer.ReportIncomingMessage(pingMessage.FarAddress.Address.ToString(), "HANDLER disc v4", $"Ping {pingMessage.SourceAddress.Address} -> {pingMessage.DestinationAddress.Address}"); - } - else - { - if (NetworkDiagTracer.IsEnabled) NetworkDiagTracer.ReportIncomingMessage(message.FarAddress.Address.ToString(), "HANDLER disc v4", message.MessageType.ToString()); - } + if (message is PingMessage pingMessage) + { + if(NetworkDiagTracer.IsEnabled) NetworkDiagTracer.ReportIncomingMessage(pingMessage.FarAddress.Address.ToString(), "HANDLER disc v4", $"PING {pingMessage.SourceAddress.Address} -> {pingMessage.DestinationAddress.Address}"); + } + else + { + if(NetworkDiagTracer.IsEnabled) NetworkDiagTracer.ReportIncomingMessage(message.FarAddress.Address.ToString(), "HANDLER disc v4", message.MessageType.ToString()); + } } - + public event EventHandler OnChannelActivated; } } \ No newline at end of file diff --git a/src/Nethermind/Nethermind.Network/IPResolver.cs b/src/Nethermind/Nethermind.Network/IPResolver.cs index fa4c4aea28a..aafe752138d 100644 --- a/src/Nethermind/Nethermind.Network/IPResolver.cs +++ b/src/Nethermind/Nethermind.Network/IPResolver.cs @@ -15,11 +15,8 @@ // along with the Nethermind. If not, see . using System; -using System.Collections; using System.Collections.Generic; -using System.Linq; using System.Net; -using System.Net.Sockets; using Nethermind.Core; using Nethermind.Logging; using Nethermind.Network.Config; diff --git a/src/Nethermind/Nethermind.Runner/Ethereum/Steps/InitializeNetwork.cs b/src/Nethermind/Nethermind.Runner/Ethereum/Steps/InitializeNetwork.cs index f11b5368040..ea01bdbbc66 100644 --- a/src/Nethermind/Nethermind.Runner/Ethereum/Steps/InitializeNetwork.cs +++ b/src/Nethermind/Nethermind.Runner/Ethereum/Steps/InitializeNetwork.cs @@ -228,6 +228,7 @@ private void InitDiscovery() SameKeyGenerator privateKeyProvider = new SameKeyGenerator(_ctx.NodeKey); DiscoveryMessageFactory discoveryMessageFactory = new DiscoveryMessageFactory(_ctx.Timestamper); NodeIdResolver nodeIdResolver = new NodeIdResolver(_ctx.EthereumEcdsa); + IPResolver ipResolver = new IPResolver(_networkConfig, _ctx.LogManager); IDiscoveryMsgSerializersProvider msgSerializersProvider = new DiscoveryMsgSerializersProvider( _ctx._messageSerializationService, @@ -261,7 +262,9 @@ private void InitDiscovery() nodeTable, discoveryStorage, discoveryConfig, - _ctx.LogManager); + _ctx.LogManager, + ipResolver + ); NodesLocator nodesLocator = new NodesLocator( nodeTable,