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);
}
}
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);
}
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);
}
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);
}
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();
}
Aggregations