Search in sources :

Example 26 with EnodeURL

use of org.hyperledger.besu.plugin.data.EnodeURL in project besu by hyperledger.

the class P2PNetworkTest method limitMaxPeers.

/**
 * Tests that max peers setting is honoured and inbound connections that would exceed the limit
 * are correctly disconnected.
 *
 * @throws Exception On Failure
 */
@Test
public void limitMaxPeers() throws Exception {
    final NodeKey nodeKey = NodeKeyUtils.generate();
    final int maxPeers = 1;
    final NetworkingConfiguration listenerConfig = NetworkingConfiguration.create().setDiscovery(DiscoveryConfiguration.create().setActive(false)).setRlpx(RlpxConfiguration.create().setBindPort(0).setMaxPeers(maxPeers).setSupportedProtocols(subProtocol()));
    try (final P2PNetwork listener = builder().nodeKey(nodeKey).config(listenerConfig).build();
        final P2PNetwork connector1 = builder().build();
        final P2PNetwork connector2 = builder().build()) {
        // Setup listener and first connection
        listener.start();
        connector1.start();
        final EnodeURL listenerEnode = listener.getLocalEnode().get();
        final Bytes listenId = listenerEnode.getNodeId();
        final int listenPort = listenerEnode.getListeningPort().get();
        final Peer listeningPeer = createPeer(listenId, listenPort);
        Assertions.assertThat(connector1.connect(listeningPeer).get(30L, TimeUnit.SECONDS).getPeerInfo().getNodeId()).isEqualTo(listenId);
        // Setup second connection and check that connection is not accepted
        final CompletableFuture<PeerConnection> peerFuture = new CompletableFuture<>();
        final CompletableFuture<DisconnectReason> reasonFuture = new CompletableFuture<>();
        connector2.subscribeDisconnect((peerConnection, reason, initiatedByPeer) -> {
            peerFuture.complete(peerConnection);
            reasonFuture.complete(reason);
        });
        connector2.start();
        Assertions.assertThat(connector2.connect(listeningPeer).get(30L, TimeUnit.SECONDS).getPeerInfo().getNodeId()).isEqualTo(listenId);
        Assertions.assertThat(peerFuture.get(30L, TimeUnit.SECONDS).getPeerInfo().getNodeId()).isEqualTo(listenId);
        assertThat(reasonFuture.get(30L, TimeUnit.SECONDS)).isEqualByComparingTo(DisconnectReason.TOO_MANY_PEERS);
    }
}
Also used : EnodeURL(org.hyperledger.besu.plugin.data.EnodeURL) Bytes(org.apache.tuweni.bytes.Bytes) CompletableFuture(java.util.concurrent.CompletableFuture) PeerConnection(org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection) DisconnectReason(org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.DisconnectMessage.DisconnectReason) Peer(org.hyperledger.besu.ethereum.p2p.peers.Peer) DefaultPeer(org.hyperledger.besu.ethereum.p2p.peers.DefaultPeer) NetworkingConfiguration(org.hyperledger.besu.ethereum.p2p.config.NetworkingConfiguration) NodeKey(org.hyperledger.besu.crypto.NodeKey) Test(org.junit.Test)

Example 27 with EnodeURL

use of org.hyperledger.besu.plugin.data.EnodeURL in project besu by hyperledger.

the class P2PPlainNetworkTest method rejectIncomingConnectionFromDisallowedPeer.

@Test
public void rejectIncomingConnectionFromDisallowedPeer() throws Exception {
    final PeerPermissions peerPermissions = mock(PeerPermissions.class);
    when(peerPermissions.isPermitted(any(), any(), any())).thenReturn(true);
    try (final P2PNetwork localNetwork = builder("partner1client1").peerPermissions(peerPermissions).build();
        final P2PNetwork remoteNetwork = builder("partner2client1").build()) {
        localNetwork.start();
        remoteNetwork.start();
        final EnodeURL localEnode = localNetwork.getLocalEnode().get();
        final Peer localPeer = DefaultPeer.fromEnodeURL(localEnode);
        final Peer remotePeer = DefaultPeer.fromEnodeURL(remoteNetwork.getLocalEnode().get());
        // Deny incoming connection permissions for remotePeer
        when(peerPermissions.isPermitted(eq(localPeer), eq(remotePeer), eq(PeerPermissions.Action.RLPX_ALLOW_NEW_INBOUND_CONNECTION))).thenReturn(false);
        // Setup disconnect listener
        final CompletableFuture<PeerConnection> peerFuture = new CompletableFuture<>();
        final CompletableFuture<DisconnectReason> reasonFuture = new CompletableFuture<>();
        remoteNetwork.subscribeDisconnect((peerConnection, reason, initiatedByPeer) -> {
            peerFuture.complete(peerConnection);
            reasonFuture.complete(reason);
        });
        // Remote connect to local
        final CompletableFuture<PeerConnection> connectFuture = remoteNetwork.connect(localPeer);
        // Check connection is made, and then a disconnect is registered at remote
        final Bytes localId = localEnode.getNodeId();
        Assertions.assertThat(connectFuture.get(5L, TimeUnit.SECONDS).getPeerInfo().getNodeId()).isEqualTo(localId);
        Assertions.assertThat(peerFuture.get(5L, TimeUnit.SECONDS).getPeerInfo().getNodeId()).isEqualTo(localId);
        assertThat(reasonFuture.get(5L, TimeUnit.SECONDS)).isEqualByComparingTo(DisconnectReason.UNKNOWN);
    }
}
Also used : EnodeURL(org.hyperledger.besu.plugin.data.EnodeURL) Bytes(org.apache.tuweni.bytes.Bytes) CompletableFuture(java.util.concurrent.CompletableFuture) PeerConnection(org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection) DisconnectReason(org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.DisconnectMessage.DisconnectReason) Peer(org.hyperledger.besu.ethereum.p2p.peers.Peer) DefaultPeer(org.hyperledger.besu.ethereum.p2p.peers.DefaultPeer) PeerPermissions(org.hyperledger.besu.ethereum.p2p.permissions.PeerPermissions) Test(org.junit.Test)

Example 28 with EnodeURL

use of org.hyperledger.besu.plugin.data.EnodeURL in project besu by hyperledger.

the class P2PPlainNetworkTest method p2pOverTlsCanHandleRecordFragmentation.

@Test
public void p2pOverTlsCanHandleRecordFragmentation() throws Exception {
    // Given
    final int tlsRecordSize = 16 * 1024;
    final int threeTlsRecords = 2 * tlsRecordSize + 1;
    final LargeMessageData largeMessageData = new LargeMessageData(Bytes.of(buildPaddedMessage(threeTlsRecords)));
    final NodeKey nodeKey = NodeKeyUtils.generate();
    try (final P2PNetwork listener = builder("partner1client1").nodeKey(nodeKey).build();
        final P2PNetwork connector = builder("partner2client1").build()) {
        final CompletableFuture<DisconnectReason> disconnectReasonFuture = new CompletableFuture<>();
        listener.subscribeDisconnect((peerConnection, reason, initiatedByPeer) -> {
            if (!DisconnectReason.CLIENT_QUITTING.equals(reason)) {
                // client quitting is the valid end state
                disconnectReasonFuture.complete(reason);
            }
        });
        final CompletableFuture<Message> successfulMessageFuture = new CompletableFuture<>();
        listener.subscribe(Capability.create("eth", 63), (capability, message) -> {
            if (message.getData().getCode() == LargeMessageData.VALID_ETH_MESSAGE_CODE) {
                successfulMessageFuture.complete(message);
            }
        });
        listener.start();
        connector.start();
        final EnodeURL listenerEnode = listener.getLocalEnode().get();
        final Bytes listenId = listenerEnode.getNodeId();
        final int listenPort = listenerEnode.getListeningPort().get();
        final PeerConnection peerConnection = connector.connect(createPeer(listenId, listenPort)).get(30000L, TimeUnit.SECONDS);
        // When
        peerConnection.sendForProtocol("eth", largeMessageData);
        // Then
        CompletableFuture.anyOf(disconnectReasonFuture, successfulMessageFuture).thenAccept(successOrFailure -> {
            if (successOrFailure instanceof DisconnectReason) {
                fail("listener disconnected due to " + ((DisconnectReason) successOrFailure).name());
            } else {
                final Message receivedMessage = (Message) successOrFailure;
                assertThat(receivedMessage.getData().getData()).isEqualTo(largeMessageData.getData());
            }
        }).get(30L, TimeUnit.SECONDS);
    }
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) EnodeURLImpl(org.hyperledger.besu.ethereum.p2p.peers.EnodeURLImpl) Arrays(java.util.Arrays) InMemoryKeyValueStorageProvider(org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SubProtocol(org.hyperledger.besu.ethereum.p2p.rlpx.wire.SubProtocol) RunWith(org.junit.runner.RunWith) Peer(org.hyperledger.besu.ethereum.p2p.peers.Peer) CompletableFuture(java.util.concurrent.CompletableFuture) NoOpMetricsSystem(org.hyperledger.besu.metrics.noop.NoOpMetricsSystem) Bytes(org.apache.tuweni.bytes.Bytes) Fail.fail(org.assertj.core.api.Fail.fail) TLSConfiguration(org.hyperledger.besu.ethereum.p2p.rlpx.connections.netty.TLSConfiguration) AbstractMessageData(org.hyperledger.besu.ethereum.p2p.rlpx.wire.AbstractMessageData) InetAddress(java.net.InetAddress) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) DefaultPeer(org.hyperledger.besu.ethereum.p2p.peers.DefaultPeer) PeerConnection(org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection) IncompatiblePeerException(org.hyperledger.besu.ethereum.p2p.network.exceptions.IncompatiblePeerException) After(org.junit.After) DiscoveryConfiguration(org.hyperledger.besu.ethereum.p2p.config.DiscoveryConfiguration) Assertions(org.assertj.core.api.Assertions) Path(java.nio.file.Path) DisconnectReason(org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.DisconnectMessage.DisconnectReason) RlpxConfiguration(org.hyperledger.besu.ethereum.p2p.config.RlpxConfiguration) NodeKeyUtils(org.hyperledger.besu.crypto.NodeKeyUtils) Charsets(com.google.common.base.Charsets) NetworkingConfiguration(org.hyperledger.besu.ethereum.p2p.config.NetworkingConfiguration) Files(java.nio.file.Files) PeerPermissionsDenylist(org.hyperledger.besu.ethereum.p2p.permissions.PeerPermissionsDenylist) Vertx(io.vertx.core.Vertx) KeyStoreWrapper(org.hyperledger.besu.pki.keystore.KeyStoreWrapper) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) File(java.io.File) EnodeURL(org.hyperledger.besu.plugin.data.EnodeURL) TimeUnit(java.util.concurrent.TimeUnit) Message(org.hyperledger.besu.ethereum.p2p.rlpx.wire.Message) Optional(java.util.Optional) PeerPermissions(org.hyperledger.besu.ethereum.p2p.permissions.PeerPermissions) Capability(org.hyperledger.besu.ethereum.p2p.rlpx.wire.Capability) Collections(java.util.Collections) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Mockito.mock(org.mockito.Mockito.mock) NodeKey(org.hyperledger.besu.crypto.NodeKey) Message(org.hyperledger.besu.ethereum.p2p.rlpx.wire.Message) PeerConnection(org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection) EnodeURL(org.hyperledger.besu.plugin.data.EnodeURL) Bytes(org.apache.tuweni.bytes.Bytes) CompletableFuture(java.util.concurrent.CompletableFuture) DisconnectReason(org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.DisconnectMessage.DisconnectReason) NodeKey(org.hyperledger.besu.crypto.NodeKey) Test(org.junit.Test)

Example 29 with EnodeURL

use of org.hyperledger.besu.plugin.data.EnodeURL in project besu by hyperledger.

the class P2PPlainNetworkTest method handshaking.

@Test
public void handshaking() throws Exception {
    final NodeKey nodeKey = NodeKeyUtils.generate();
    try (final P2PNetwork listener = builder("partner1client1").nodeKey(nodeKey).build();
        final P2PNetwork connector = builder("partner2client1").build()) {
        listener.start();
        connector.start();
        final EnodeURL listenerEnode = listener.getLocalEnode().get();
        final Bytes listenId = listenerEnode.getNodeId();
        final int listenPort = listenerEnode.getListeningPort().get();
        Assertions.assertThat(connector.connect(createPeer(listenId, listenPort)).get(30L, TimeUnit.SECONDS).getPeerInfo().getNodeId()).isEqualTo(listenId);
    }
}
Also used : EnodeURL(org.hyperledger.besu.plugin.data.EnodeURL) Bytes(org.apache.tuweni.bytes.Bytes) NodeKey(org.hyperledger.besu.crypto.NodeKey) Test(org.junit.Test)

Example 30 with EnodeURL

use of org.hyperledger.besu.plugin.data.EnodeURL in project besu by hyperledger.

the class P2PPlainNetworkTest method rejectPeerWithNoSharedCaps.

@Test
public void rejectPeerWithNoSharedCaps() throws Exception {
    final NodeKey listenerNodeKey = NodeKeyUtils.generate();
    final NodeKey connectorNodeKey = NodeKeyUtils.generate();
    final SubProtocol subprotocol1 = subProtocol("eth");
    final Capability cap1 = Capability.create(subprotocol1.getName(), 63);
    final SubProtocol subprotocol2 = subProtocol("oth");
    final Capability cap2 = Capability.create(subprotocol2.getName(), 63);
    try (final P2PNetwork listener = builder("partner1client1").nodeKey(listenerNodeKey).supportedCapabilities(cap1).build();
        final P2PNetwork connector = builder("partner2client1").nodeKey(connectorNodeKey).supportedCapabilities(cap2).build()) {
        listener.start();
        connector.start();
        final EnodeURL listenerEnode = listener.getLocalEnode().get();
        final Bytes listenId = listenerEnode.getNodeId();
        final int listenPort = listenerEnode.getListeningPort().get();
        final Peer listenerPeer = createPeer(listenId, listenPort);
        final CompletableFuture<PeerConnection> connectFuture = connector.connect(listenerPeer);
        assertThatThrownBy(connectFuture::get).hasCauseInstanceOf(IncompatiblePeerException.class);
    }
}
Also used : EnodeURL(org.hyperledger.besu.plugin.data.EnodeURL) Bytes(org.apache.tuweni.bytes.Bytes) Capability(org.hyperledger.besu.ethereum.p2p.rlpx.wire.Capability) PeerConnection(org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection) SubProtocol(org.hyperledger.besu.ethereum.p2p.rlpx.wire.SubProtocol) Peer(org.hyperledger.besu.ethereum.p2p.peers.Peer) DefaultPeer(org.hyperledger.besu.ethereum.p2p.peers.DefaultPeer) NodeKey(org.hyperledger.besu.crypto.NodeKey) Test(org.junit.Test)

Aggregations

EnodeURL (org.hyperledger.besu.plugin.data.EnodeURL)109 Test (org.junit.Test)83 Bytes (org.apache.tuweni.bytes.Bytes)26 PeerConnection (org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection)22 DefaultPeer (org.hyperledger.besu.ethereum.p2p.peers.DefaultPeer)17 Peer (org.hyperledger.besu.ethereum.p2p.peers.Peer)17 NodeKey (org.hyperledger.besu.crypto.NodeKey)14 Path (java.nio.file.Path)12 CompletableFuture (java.util.concurrent.CompletableFuture)12 Optional (java.util.Optional)11 List (java.util.List)10 Collectors (java.util.stream.Collectors)10 URL (java.net.URL)9 EnodeURLImpl (org.hyperledger.besu.ethereum.p2p.peers.EnodeURLImpl)9 DisconnectReason (org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.DisconnectMessage.DisconnectReason)9 File (java.io.File)8 Collections (java.util.Collections)8 LocalPermissioningConfiguration (org.hyperledger.besu.ethereum.permissioning.LocalPermissioningConfiguration)8 ArrayList (java.util.ArrayList)7 EthNetworkConfig (org.hyperledger.besu.cli.config.EthNetworkConfig)7