Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(discovery): makes pong send only IP without hostname #2279

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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());
jurajpiar marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
logger.debug("handlePong - Peer discovery request with id [{}] is either null or invalid", message.getMessageId());
Expand Down
28 changes: 24 additions & 4 deletions rskj-core/src/test/java/co/rsk/net/discovery/PeerExplorerTest.java
jurajpiar marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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));
Expand All @@ -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();
}

Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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<Node> discoveredNode = peerExplorer.getNodes()
.stream().filter((Node node) -> Objects.equals(
node.getAddress().getAddress().getHostAddress(),
localhostIP))
.findFirst();
assertTrue(discoveredNode.isPresent());
assertNotEquals(HOST_1, discoveredNode.get().getHost());
}
}
Loading