Search in sources :

Example 21 with NodeKey

use of org.hyperledger.besu.crypto.NodeKey in project besu by hyperledger.

the class P2PNetworkTest method preventMultipleConnections.

@Test
public void preventMultipleConnections() throws Exception {
    final NodeKey listenNodeKey = NodeKeyUtils.generate();
    try (final P2PNetwork listener = builder().nodeKey(listenNodeKey).build();
        final P2PNetwork connector = builder().build()) {
        listener.start();
        connector.start();
        final EnodeURL listenerEnode = listener.getLocalEnode().get();
        final Bytes listenId = listenerEnode.getNodeId();
        final int listenPort = listenerEnode.getListeningPort().get();
        final CompletableFuture<PeerConnection> firstFuture = connector.connect(createPeer(listenId, listenPort));
        final CompletableFuture<PeerConnection> secondFuture = connector.connect(createPeer(listenId, listenPort));
        final PeerConnection firstConnection = firstFuture.get(30L, TimeUnit.SECONDS);
        final PeerConnection secondConnection = secondFuture.get(30L, TimeUnit.SECONDS);
        Assertions.assertThat(firstConnection.getPeerInfo().getNodeId()).isEqualTo(listenId);
        // Connections should reference the same instance - i.e. we shouldn't create 2 distinct
        // connections
        assertThat(firstConnection == secondConnection).isTrue();
    }
}
Also used : EnodeURL(org.hyperledger.besu.plugin.data.EnodeURL) Bytes(org.apache.tuweni.bytes.Bytes) PeerConnection(org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection) NodeKey(org.hyperledger.besu.crypto.NodeKey) Test(org.junit.Test)

Example 22 with NodeKey

use of org.hyperledger.besu.crypto.NodeKey in project besu by hyperledger.

the class P2PNetworkTest 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().nodeKey(listenerNodeKey).supportedCapabilities(cap1).build();
        final P2PNetwork connector = builder().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)

Example 23 with NodeKey

use of org.hyperledger.besu.crypto.NodeKey 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 24 with NodeKey

use of org.hyperledger.besu.crypto.NodeKey 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 25 with NodeKey

use of org.hyperledger.besu.crypto.NodeKey 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)

Aggregations

NodeKey (org.hyperledger.besu.crypto.NodeKey)94 Test (org.junit.Test)78 Address (org.hyperledger.besu.datatypes.Address)43 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)31 BlockHeaderValidator (org.hyperledger.besu.ethereum.mainnet.BlockHeaderValidator)24 Bytes (org.apache.tuweni.bytes.Bytes)18 DiscoveryPeer (org.hyperledger.besu.ethereum.p2p.discovery.DiscoveryPeer)17 Endpoint (org.hyperledger.besu.ethereum.p2p.discovery.Endpoint)16 ProtocolContext (org.hyperledger.besu.ethereum.ProtocolContext)14 List (java.util.List)13 ConsensusRoundIdentifier (org.hyperledger.besu.consensus.common.bft.ConsensusRoundIdentifier)13 EnodeURL (org.hyperledger.besu.plugin.data.EnodeURL)13 Optional (java.util.Optional)12 NodeKeyUtils (org.hyperledger.besu.crypto.NodeKeyUtils)11 Collectors (java.util.stream.Collectors)10 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)10 Util (org.hyperledger.besu.ethereum.core.Util)9 PeerPermissionsDenylist (org.hyperledger.besu.ethereum.p2p.permissions.PeerPermissionsDenylist)9 Before (org.junit.Before)9 MessageFactory (org.hyperledger.besu.consensus.ibft.payload.MessageFactory)8