From 2bd1e0d1d004e4b1f212067fddc7be64417e40c9 Mon Sep 17 00:00:00 2001 From: Juraj Piar Date: Mon, 1 Apr 2024 09:29:12 +0100 Subject: [PATCH] fix(discovery): makes pong send only IP squashme: makes safer test --- .../co/rsk/net/discovery/PeerExplorer.java | 2 +- .../rsk/net/discovery/PeerExplorerTest.java | 28 ++++++++++++++++--- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/rskj-core/src/main/java/co/rsk/net/discovery/PeerExplorer.java b/rskj-core/src/main/java/co/rsk/net/discovery/PeerExplorer.java index 17b7d8cba8b..97db67b4947 100644 --- a/rskj-core/src/main/java/co/rsk/net/discovery/PeerExplorer.java +++ b/rskj-core/src/main/java/co/rsk/net/discovery/PeerExplorer.java @@ -259,7 +259,7 @@ private void handlePong(InetSocketAddress pongAddress, PongPeerMessage message) this.pendingPingRequests.remove(message.getMessageId()); NodeChallenge challenge = this.challengeManager.removeChallenge(message.getMessageId()); if (challenge == null) { - this.addConnection(message, request.getAddress().getHostString(), request.getAddress().getPort()); + this.addConnection(message, request.getAddress().getAddress().getHostAddress(), request.getAddress().getPort()); } } else { logger.debug("handlePong - Peer discovery request with id [{}] is either null or invalid", message.getMessageId()); diff --git a/rskj-core/src/test/java/co/rsk/net/discovery/PeerExplorerTest.java b/rskj-core/src/test/java/co/rsk/net/discovery/PeerExplorerTest.java index 2a49c6d1c91..70d4b61df2c 100644 --- a/rskj-core/src/test/java/co/rsk/net/discovery/PeerExplorerTest.java +++ b/rskj-core/src/test/java/co/rsk/net/discovery/PeerExplorerTest.java @@ -144,7 +144,7 @@ void handlePingMessageFromDifferentNetwork() throws Exception { ECKey key1 = ECKey.fromPrivate(Hex.decode(KEY_1)).decompress(); String check = UUID.randomUUID().toString(); - PingPeerMessage pingPeerMessageWithDifferentNetwork = PingPeerMessage.create(HOST_1, PORT_1, check, key1, this.NETWORK_ID2); + PingPeerMessage pingPeerMessageWithDifferentNetwork = PingPeerMessage.create(HOST_1, PORT_1, check, key1, NETWORK_ID2); DiscoveryEvent incomingPingEvent = new DiscoveryEvent(pingPeerMessageWithDifferentNetwork, new InetSocketAddress(HOST_1, PORT_1)); //A message is received @@ -176,7 +176,7 @@ void handlePingMessage() throws Exception { ECKey key1 = ECKey.fromPrivate(Hex.decode(KEY_1)).decompress(); String check = UUID.randomUUID().toString(); - PingPeerMessage nodeMessage = PingPeerMessage.create(HOST_1, PORT_1, check, key1, this.NETWORK_ID1); + PingPeerMessage nodeMessage = PingPeerMessage.create(HOST_1, PORT_1, check, key1, NETWORK_ID1); DiscoveryEvent incomingPingEvent = new DiscoveryEvent(nodeMessage, new InetSocketAddress(HOST_2, PORT_3)); //A message is received @@ -234,7 +234,7 @@ void handlePongMessage() throws Exception { peerExplorer.start(false); - //A incoming pong for a Ping we did not sent. + //An incoming pong for a Ping we did not sent. String check = UUID.randomUUID().toString(); PongPeerMessage incomingPongMessage = PongPeerMessage.create(HOST_1, PORT_1, check, key1, NETWORK_ID1); DiscoveryEvent incomingPongEvent = new DiscoveryEvent(incomingPongMessage, new InetSocketAddress(HOST_1, PORT_1)); @@ -257,13 +257,17 @@ void handlePongMessage() throws Exception { addedNodes = peerExplorer.getNodes(); assertEquals(1, addedNodes.size()); - //A incoming pong for a ping we sent but from a different address + //An incoming pong for a ping we sent but from a different address incomingPongMessage = PongPeerMessage.create(HOST_4, PORT_4, ((PingPeerMessage) initialPingMessages.get(1).getMessage()).getMessageId(), key4, NETWORK_ID1); incomingPongEvent = new DiscoveryEvent(incomingPongMessage, new InetSocketAddress(HOST_1, PORT_1)); channel.clearEvents(); channel.channelRead0(ctx, incomingPongEvent); assertEquals(1, peerExplorer.getNodes().size()); assertEquals(1,peerExplorer.getKnownHosts().size()); + + // Verify that only IP is being sent, not hostname + assertNoHostnameFromDiscovery(peerExplorer); + peerExplorer.dispose(); } @@ -426,6 +430,9 @@ void handleFindNodeMessage() throws Exception { NeighborsPeerMessage neighborsPeerMessage = (NeighborsPeerMessage) sentEvents.get(0).getMessage(); assertEquals(1, neighborsPeerMessage.getNodes().size()); + // Verify that only IP is being sent, not hostname + assertNoHostnameFromDiscovery(peerExplorer); + peerExplorer.dispose(); } @@ -763,4 +770,17 @@ private static InetAddress parseAddress(String host) { throw new IllegalArgumentException(e); } } + + private static void assertNoHostnameFromDiscovery(PeerExplorer peerExplorer) { + ECKey key = ECKey.fromPrivate(Hex.decode(KEY_1)).decompress(); + Node localhostNode = new Node(key.getNodeId(), HOST_1, PORT_1); // more stable if loopback address overrides + String localhostIP = localhostNode.getAddress().getAddress().getHostAddress(); + Optional discoveredNode = peerExplorer.getNodes() + .stream().filter((Node node) -> Objects.equals( + node.getAddress().getAddress().getHostAddress(), + localhostIP)) + .findFirst(); + assertTrue(discoveredNode.isPresent()); + assertNotEquals(HOST_1, discoveredNode.get().getHost()); + } }