Search in sources :

Example 1 with DisconnectReason

use of org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.DisconnectMessage.DisconnectReason in project besu by hyperledger.

the class P2PPlainNetworkTest method rejectIncomingConnectionFromBlacklistedPeer.

@Test
public void rejectIncomingConnectionFromBlacklistedPeer() throws Exception {
    final PeerPermissionsDenylist localBlacklist = PeerPermissionsDenylist.create();
    try (final P2PNetwork localNetwork = builder("partner1client1").peerPermissions(localBlacklist).build();
        final P2PNetwork remoteNetwork = builder("partner2client1").build()) {
        localNetwork.start();
        remoteNetwork.start();
        final EnodeURL localEnode = localNetwork.getLocalEnode().get();
        final Bytes localId = localEnode.getNodeId();
        final int localPort = localEnode.getListeningPort().get();
        final EnodeURL remoteEnode = remoteNetwork.getLocalEnode().get();
        final Bytes remoteId = remoteEnode.getNodeId();
        final int remotePort = remoteEnode.getListeningPort().get();
        final Peer localPeer = createPeer(localId, localPort);
        final Peer remotePeer = createPeer(remoteId, remotePort);
        // Blacklist the remote peer
        localBlacklist.add(remotePeer);
        // 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
        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) PeerPermissionsDenylist(org.hyperledger.besu.ethereum.p2p.permissions.PeerPermissionsDenylist) 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) Test(org.junit.Test)

Example 2 with DisconnectReason

use of org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.DisconnectMessage.DisconnectReason in project besu by hyperledger.

the class RlpxConnectionTest method disconnect_activeOutboundConnection.

@Test
public void disconnect_activeOutboundConnection() {
    final Peer peer = createPeer();
    final CompletableFuture<PeerConnection> future = new CompletableFuture<>();
    final RlpxConnection conn = RlpxConnection.outboundConnection(peer, future);
    final PeerConnection peerConnection = peerConnection(peer);
    future.complete(peerConnection);
    final DisconnectReason reason = DisconnectReason.REQUESTED;
    conn.disconnect(reason);
    // Check disconnect was issued
    assertThat(conn.isFailedOrDisconnected()).isTrue();
    verify(peerConnection).disconnect(reason);
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) DisconnectReason(org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.DisconnectMessage.DisconnectReason) PeerTestHelper.createPeer(org.hyperledger.besu.ethereum.p2p.peers.PeerTestHelper.createPeer) Peer(org.hyperledger.besu.ethereum.p2p.peers.Peer) Test(org.junit.Test)

Example 3 with DisconnectReason

use of org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.DisconnectMessage.DisconnectReason in project besu by hyperledger.

the class AbstractPeerConnectionTest method disconnect.

@Test
public void disconnect() {
    final AtomicBoolean disconnectCallbackInvoked = new AtomicBoolean(false);
    final DisconnectReason disconnectReason = DisconnectReason.USELESS_PEER;
    connectionEvents.subscribeDisconnect((conn, reason, fromPeer) -> {
        disconnectCallbackInvoked.set(true);
        assertThat(reason).isEqualTo(disconnectReason);
        assertThat(conn).isEqualTo(connection);
        assertThat(fromPeer).isFalse();
        // Check the state of the connection as seen by disconnect handlers
        assertThat(conn.isDisconnected()).isTrue();
        assertThatThrownBy(() -> connection.send(null, PingMessage.get()));
    });
    connection.disconnect(disconnectReason);
    assertThat(disconnectCallbackInvoked).isTrue();
    assertThat(connection.isDisconnected()).isTrue();
    assertThat(connection.closedCount).isEqualTo(1);
    assertThat(connection.sentMessages.size()).isEqualTo(1);
    assertThat(connection.sentMessages.get(0).messageData.getCode()).isEqualTo(WireMessageCodes.DISCONNECT);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DisconnectReason(org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.DisconnectMessage.DisconnectReason) Test(org.junit.Test)

Example 4 with DisconnectReason

use of org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.DisconnectMessage.DisconnectReason in project besu by hyperledger.

the class RlpxConnectionTest method disconnect_inboundConnection.

@Test
public void disconnect_inboundConnection() {
    final Peer peer = createPeer();
    final PeerConnection peerConnection = peerConnection(peer);
    final RlpxConnection conn = RlpxConnection.inboundConnection(peerConnection);
    assertThat(conn.isFailedOrDisconnected()).isFalse();
    final DisconnectReason reason = DisconnectReason.REQUESTED;
    conn.disconnect(reason);
    // Check disconnect was issued
    assertThat(conn.isFailedOrDisconnected()).isTrue();
    verify(peerConnection).disconnect(reason);
}
Also used : DisconnectReason(org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.DisconnectMessage.DisconnectReason) PeerTestHelper.createPeer(org.hyperledger.besu.ethereum.p2p.peers.PeerTestHelper.createPeer) Peer(org.hyperledger.besu.ethereum.p2p.peers.Peer) Test(org.junit.Test)

Example 5 with DisconnectReason

use of org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.DisconnectMessage.DisconnectReason in project besu by hyperledger.

the class RlpxConnectionTest method disconnect_failedOutboundConnection.

@Test
public void disconnect_failedOutboundConnection() {
    final Peer peer = createPeer();
    final CompletableFuture<PeerConnection> future = new CompletableFuture<>();
    final RlpxConnection conn = RlpxConnection.outboundConnection(peer, future);
    future.completeExceptionally(new IllegalStateException("whoops"));
    assertThat(conn.isFailedOrDisconnected()).isTrue();
    final DisconnectReason reason = DisconnectReason.REQUESTED;
    conn.disconnect(reason);
    assertThat(conn.isFailedOrDisconnected()).isTrue();
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) DisconnectReason(org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.DisconnectMessage.DisconnectReason) PeerTestHelper.createPeer(org.hyperledger.besu.ethereum.p2p.peers.PeerTestHelper.createPeer) Peer(org.hyperledger.besu.ethereum.p2p.peers.Peer) Test(org.junit.Test)

Aggregations

DisconnectReason (org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.DisconnectMessage.DisconnectReason)21 Test (org.junit.Test)20 Peer (org.hyperledger.besu.ethereum.p2p.peers.Peer)15 CompletableFuture (java.util.concurrent.CompletableFuture)13 PeerConnection (org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection)12 DefaultPeer (org.hyperledger.besu.ethereum.p2p.peers.DefaultPeer)11 Bytes (org.apache.tuweni.bytes.Bytes)10 EnodeURL (org.hyperledger.besu.plugin.data.EnodeURL)9 PeerTestHelper.createPeer (org.hyperledger.besu.ethereum.p2p.peers.PeerTestHelper.createPeer)5 PeerPermissionsDenylist (org.hyperledger.besu.ethereum.p2p.permissions.PeerPermissionsDenylist)5 MessageData (org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData)4 RawMessage (org.hyperledger.besu.ethereum.p2p.rlpx.wire.RawMessage)4 NodeKey (org.hyperledger.besu.crypto.NodeKey)3 NetworkingConfiguration (org.hyperledger.besu.ethereum.p2p.config.NetworkingConfiguration)3 PeerPermissions (org.hyperledger.besu.ethereum.p2p.permissions.PeerPermissions)3 Arrays (java.util.Arrays)2 Optional (java.util.Optional)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Assertions (org.assertj.core.api.Assertions)2 EnodeURLImpl (org.hyperledger.besu.ethereum.p2p.peers.EnodeURLImpl)2