diff --git a/ethereum/p2p/src/main/java/tech/pegasys/pantheon/ethereum/p2p/netty/NettyP2PNetwork.java b/ethereum/p2p/src/main/java/tech/pegasys/pantheon/ethereum/p2p/netty/NettyP2PNetwork.java index d39a971cc8..ed39ee5d88 100644 --- a/ethereum/p2p/src/main/java/tech/pegasys/pantheon/ethereum/p2p/netty/NettyP2PNetwork.java +++ b/ethereum/p2p/src/main/java/tech/pegasys/pantheon/ethereum/p2p/netty/NettyP2PNetwork.java @@ -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; @@ -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); @@ -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); diff --git a/ethereum/p2p/src/test/java/tech/pegasys/pantheon/ethereum/p2p/netty/NettyP2PNetworkTest.java b/ethereum/p2p/src/test/java/tech/pegasys/pantheon/ethereum/p2p/netty/NettyP2PNetworkTest.java index b5bc63aac7..a648cc5bf5 100644 --- a/ethereum/p2p/src/test/java/tech/pegasys/pantheon/ethereum/p2p/netty/NettyP2PNetworkTest.java +++ b/ethereum/p2p/src/test/java/tech/pegasys/pantheon/ethereum/p2p/netty/NettyP2PNetworkTest.java @@ -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; @@ -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); @@ -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()); @@ -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(); @@ -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 future = nettyP2PNetwork.connect(remotePeer); @@ -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; } @@ -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( @@ -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 {