Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Simplify enode construction #1283

Merged
merged 3 commits into from
Apr 16, 2019
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 @@ -48,7 +48,6 @@
import tech.pegasys.pantheon.util.bytes.BytesValue;
import tech.pegasys.pantheon.util.enode.EnodeURL;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -632,11 +631,8 @@ private boolean isPeerConnectionAllowed(final PeerConnection peerConnection) {
return nodePermissioningController
.map(
c -> {
final EnodeURL localPeerEnodeURL =
peerInfoToEnodeURL(ourPeerInfo, peerConnection.getLocalAddress());
final EnodeURL remotePeerEnodeURL =
peerInfoToEnodeURL(
peerConnection.getPeerInfo(), peerConnection.getRemoteAddress());
final EnodeURL localPeerEnodeURL = getSelfEnodeURL().orElse(buildSelfEnodeURL());
final EnodeURL remotePeerEnodeURL = peerConnection.getRemoteEnode();
return c.isPermitted(localPeerEnodeURL, remotePeerEnodeURL);
})
.orElse(true);
Expand All @@ -652,17 +648,6 @@ private boolean isPeerAllowed(final Peer peer) {
.orElse(true);
}

private EnodeURL peerInfoToEnodeURL(final PeerInfo ourPeerInfo, final InetSocketAddress address) {
final BytesValue localNodeId = ourPeerInfo.getNodeId();
final InetAddress localHostAddress = address.getAddress();
final int localPort = ourPeerInfo.getPort();
return EnodeURL.builder()
.nodeId(localNodeId)
.ipAddress(localHostAddress)
.listeningPort(localPort)
.build();
}

@VisibleForTesting
boolean isConnecting(final Peer peer) {
return pendingConnections.containsKey(peer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
import tech.pegasys.pantheon.util.enode.EnodeURL;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
Expand Down Expand Up @@ -706,12 +705,11 @@ public void whenStoppingNetworkWithNodePermissioningShouldUnsubscribeBlockAddedE
public void onBlockAddedShouldCheckPermissionsForAllPeers() {
final BlockAddedEvent blockAddedEvent = blockAddedEvent();
final NettyP2PNetwork nettyP2PNetwork = nettyP2PNetwork();
final Peer localPeer = mockPeer("127.0.0.1", 30301);
final Peer remotePeer1 = mockPeer("127.0.0.2", 30302);
final Peer remotePeer2 = mockPeer("127.0.0.3", 30303);

final PeerConnection peerConnection1 = mockPeerConnection(localPeer, remotePeer1);
final PeerConnection peerConnection2 = mockPeerConnection(localPeer, remotePeer2);
final PeerConnection peerConnection1 = mockPeerConnection(remotePeer1);
final PeerConnection peerConnection2 = mockPeerConnection(remotePeer2);

nettyP2PNetwork.start();
nettyP2PNetwork.connect(remotePeer1).complete(peerConnection1);
Expand All @@ -728,13 +726,11 @@ public void onBlockAddedAndPeerNotPermittedShouldDisconnect() {
final BlockAddedEvent blockAddedEvent = blockAddedEvent();
final NettyP2PNetwork nettyP2PNetwork = nettyP2PNetwork();

final Peer localPeer = mockPeer("127.0.0.1", 30301);
final Peer permittedPeer = mockPeer("127.0.0.2", 30302);
final Peer notPermittedPeer = mockPeer("127.0.0.3", 30303);

final PeerConnection permittedPeerConnection = mockPeerConnection(localPeer, permittedPeer);
final PeerConnection notPermittedPeerConnection =
mockPeerConnection(localPeer, notPermittedPeer);
final PeerConnection permittedPeerConnection = mockPeerConnection(permittedPeer);
final PeerConnection notPermittedPeerConnection = mockPeerConnection(notPermittedPeer);

final EnodeURL permittedEnodeURL = EnodeURL.fromString(permittedPeer.getEnodeURLString());
final EnodeURL notPermittedEnodeURL = EnodeURL.fromString(notPermittedPeer.getEnodeURLString());
Expand Down Expand Up @@ -764,9 +760,8 @@ public void removePeerReturnsTrueIfNodeWasInMaintaineConnectionsAndDisconnectsIf
final NettyP2PNetwork nettyP2PNetwork = nettyP2PNetwork();
nettyP2PNetwork.start();

final Peer localPeer = mockPeer("127.0.0.1", 30301);
final Peer remotePeer = mockPeer("127.0.0.2", 30302);
final PeerConnection peerConnection = mockPeerConnection(localPeer, remotePeer);
final PeerConnection peerConnection = mockPeerConnection(remotePeer);

nettyP2PNetwork.addMaintainConnectionPeer(remotePeer);
assertThat(nettyP2PNetwork.peerMaintainConnectionList.contains(remotePeer)).isTrue();
Expand All @@ -787,9 +782,8 @@ public void removePeerReturnsFalseIfNotInMaintainedListButDisconnectsPeer() {
final NettyP2PNetwork nettyP2PNetwork = nettyP2PNetwork();
nettyP2PNetwork.start();

final Peer localPeer = mockPeer("127.0.0.1", 30301);
final Peer remotePeer = mockPeer("127.0.0.2", 30302);
final PeerConnection peerConnection = mockPeerConnection(localPeer, remotePeer);
final PeerConnection peerConnection = mockPeerConnection(remotePeer);

CompletableFuture<PeerConnection> future = nettyP2PNetwork.connect(remotePeer);

Expand Down Expand Up @@ -979,24 +973,20 @@ private PeerConnection mockPeerConnection() {
return mockPeerConnection(BytesValue.fromHexString("0x00"));
}

private PeerConnection mockPeerConnection(final Peer localPeer, final Peer remotePeer) {
final PeerInfo peerInfo = mock(PeerInfo.class);
doReturn(remotePeer.getId()).when(peerInfo).getNodeId();
doReturn(remotePeer.getEndpoint().getTcpPort().getAsInt()).when(peerInfo).getPort();
private PeerConnection mockPeerConnection(final Peer remotePeer) {
final EnodeURL remoteEnode = remotePeer.getEnodeURL();
final PeerInfo peerInfo =
new PeerInfo(
5,
"test",
Arrays.asList(Capability.create("eth", 63)),
remoteEnode.getListeningPort(),
remoteEnode.getNodeId());

final PeerConnection peerConnection = mock(PeerConnection.class);
when(peerConnection.getRemoteEnode()).thenReturn(remoteEnode);
when(peerConnection.getPeerInfo()).thenReturn(peerInfo);

Endpoint localEndpoint = localPeer.getEndpoint();
InetSocketAddress localSocketAddress =
new InetSocketAddress(localEndpoint.getHost(), localEndpoint.getTcpPort().getAsInt());
when(peerConnection.getLocalAddress()).thenReturn(localSocketAddress);

Endpoint remoteEndpoint = remotePeer.getEndpoint();
InetSocketAddress remoteSocketAddress =
new InetSocketAddress(remoteEndpoint.getHost(), remoteEndpoint.getTcpPort().getAsInt());
when(peerConnection.getRemoteAddress()).thenReturn(remoteSocketAddress);

return peerConnection;
}

Expand Down Expand Up @@ -1049,7 +1039,6 @@ private Peer mockPeer(final String host, final int port) {
}

private Peer mockPeer(final BytesValue id, final String host, final int port) {
final Peer peer = mock(Peer.class);
final Endpoint endpoint = new Endpoint(host, port, OptionalInt.of(port));
final String enodeURL =
String.format(
Expand All @@ -1059,11 +1048,7 @@ private Peer mockPeer(final BytesValue id, final String host, final int port) {
endpoint.getUdpPort(),
endpoint.getTcpPort().getAsInt());

when(peer.getId()).thenReturn(id);
when(peer.getEndpoint()).thenReturn(endpoint);
when(peer.getEnodeURLString()).thenReturn(enodeURL);

return peer;
return DefaultPeer.fromURI(enodeURL);
}

public static class EnodeURLMatcher implements ArgumentMatcher<EnodeURL> {
Expand Down