Simplify enode construction (#1283)

Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>
pull/2/head
mbaxter 6 years ago committed by GitHub
parent 31a5437080
commit 8fc9ee7243
  1. 19
      ethereum/p2p/src/main/java/tech/pegasys/pantheon/ethereum/p2p/netty/NettyP2PNetwork.java
  2. 49
      ethereum/p2p/src/test/java/tech/pegasys/pantheon/ethereum/p2p/netty/NettyP2PNetworkTest.java

@ -48,7 +48,6 @@ import tech.pegasys.pantheon.util.Subscribers;
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 @@ public class NettyP2PNetwork implements P2PNetwork {
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 @@ public class NettyP2PNetwork implements P2PNetwork {
.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);

@ -59,7 +59,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.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
@ -706,12 +705,11 @@ public final class NettyP2PNetworkTest {
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 final class NettyP2PNetworkTest {
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 final class NettyP2PNetworkTest {
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 final class NettyP2PNetworkTest {
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);
@ -979,24 +973,20 @@ public final class NettyP2PNetworkTest {
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 @@ public final class NettyP2PNetworkTest {
}
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 @@ public final class NettyP2PNetworkTest {
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> {

Loading…
Cancel
Save