use of org.hyperledger.besu.ethereum.p2p.rlpx.connections.MockPeerConnection in project besu by hyperledger.
the class RlpxAgentTest method incomingConnection_succeedsEventuallyWithRandomPeerPrioritization.
@Test
public void incomingConnection_succeedsEventuallyWithRandomPeerPrioritization() {
// Saturate connections with one local and one remote
final int maxPeers = 25;
startAgentWithMaxPeers(maxPeers, builder -> builder.randomPeerPriority(true), rlpxConfiguration -> rlpxConfiguration.setLimitRemoteWireConnectionsEnabled(false));
agent.connect(createPeer());
for (int i = 0; i < 24; i++) {
connectionInitializer.simulateIncomingConnection(connection(createPeer()));
}
// Sanity check
assertThat(agent.getConnectionCount()).isEqualTo(maxPeers);
boolean newConnectionDisconnected = false;
boolean oldConnectionDisconnected = false;
// With very high probability we should see the connections churn
for (int i = 0; i < 1000 && !(newConnectionDisconnected && oldConnectionDisconnected); ++i) {
final List<PeerConnection> connectionsBefore = agent.streamConnections().collect(Collectors.toUnmodifiableList());
// Simulate incoming connection
final Peer newPeer = createPeer();
final MockPeerConnection incomingConnection = connection(newPeer);
connectionInitializer.simulateIncomingConnection(incomingConnection);
final List<PeerConnection> connectionsAfter = agent.streamConnections().collect(Collectors.toUnmodifiableList());
if (connectionsBefore.equals(connectionsAfter)) {
newConnectionDisconnected = true;
} else if (!connectionsBefore.equals(connectionsAfter)) {
oldConnectionDisconnected = true;
}
assertThat(agent.getConnectionCount()).isEqualTo(maxPeers);
}
assertThat(newConnectionDisconnected).isTrue();
assertThat(oldConnectionDisconnected).isTrue();
}
use of org.hyperledger.besu.ethereum.p2p.rlpx.connections.MockPeerConnection in project besu by hyperledger.
the class RlpxAgentTest method incomingConnection_maxPeersExceeded.
@Test
public void incomingConnection_maxPeersExceeded() throws ExecutionException, InterruptedException {
// Saturate connections
startAgentWithMaxPeers(1);
final Peer existingPeer = createPeer();
final PeerConnection existingConnection = agent.connect(existingPeer).get();
// Sanity check
assertThat(agent.getConnectionCount()).isEqualTo(1);
// Simulate incoming connection
final Peer newPeer = createPeer();
final MockPeerConnection incomingConnection = connection(newPeer);
connectionInitializer.simulateIncomingConnection(incomingConnection);
// Incoming or existing connection should be disconnected
assertThat(agent.getConnectionCount()).isEqualTo(1);
if (agent.getPeerConnection(newPeer).isPresent()) {
assertThat(((MockPeerConnection) existingConnection).getDisconnectReason()).contains(DisconnectReason.TOO_MANY_PEERS);
} else {
assertThat(incomingConnection.getDisconnectReason()).contains(DisconnectReason.TOO_MANY_PEERS);
}
}
use of org.hyperledger.besu.ethereum.p2p.rlpx.connections.MockPeerConnection in project besu by hyperledger.
the class RlpxAgentTest method incomingConnection_deduplicatedWhenAlreadyConnected_peerWithHigherValueNodeId.
@Test
public void incomingConnection_deduplicatedWhenAlreadyConnected_peerWithHigherValueNodeId() {
final Bytes localNodeId = Bytes.fromHexString("0x01", EnodeURLImpl.NODE_ID_SIZE);
final Bytes remoteNodeId = Bytes.fromHexString("0x02", EnodeURLImpl.NODE_ID_SIZE);
startAgent(localNodeId);
final Peer peer = createPeer(remoteNodeId);
final CompletableFuture<PeerConnection> existingConnection = agent.connect(peer);
final MockPeerConnection incomingConnection = connection(peer);
connectionInitializer.simulateIncomingConnection(incomingConnection);
// Existing connection should be kept
assertThat(agent.getPeerConnection(peer)).contains(existingConnection);
assertThat(agent.getConnectionCount()).isEqualTo(1);
assertThat(incomingConnection.isDisconnected()).isTrue();
assertThat(incomingConnection.getDisconnectReason()).contains(DisconnectReason.ALREADY_CONNECTED);
}
use of org.hyperledger.besu.ethereum.p2p.rlpx.connections.MockPeerConnection in project besu by hyperledger.
the class RlpxAgentTest method incomingConnection_afterMaxRemotelyInitiatedConnectionsHaveBeenEstablished.
@Test
public void incomingConnection_afterMaxRemotelyInitiatedConnectionsHaveBeenEstablished() {
final int maxPeers = 10;
final int maxRemotePeers = 8;
final float maxRemotePeersFraction = (float) maxRemotePeers / (float) maxPeers;
config.setLimitRemoteWireConnectionsEnabled(true);
config.setFractionRemoteWireConnectionsAllowed(maxRemotePeersFraction);
startAgentWithMaxPeers(maxPeers);
// Connect max remote peers
for (int i = 0; i < maxRemotePeers; i++) {
final Peer remotelyInitiatedPeer = createPeer();
final MockPeerConnection incomingConnection = connection(remotelyInitiatedPeer);
connectionInitializer.simulateIncomingConnection(incomingConnection);
assertThat(incomingConnection.getDisconnectReason()).isEmpty();
}
// Next remote connection should be rejected
final Peer remotelyInitiatedPeer = createPeer();
final MockPeerConnection incomingConnection = connection(remotelyInitiatedPeer);
connectionInitializer.simulateIncomingConnection(incomingConnection);
assertThat(incomingConnection.getDisconnectReason()).contains(DisconnectReason.TOO_MANY_PEERS);
}
use of org.hyperledger.besu.ethereum.p2p.rlpx.connections.MockPeerConnection in project besu by hyperledger.
the class RlpxAgentTest method stop.
@Test
public void stop() throws ExecutionException, InterruptedException {
// Cannot stop before starting
final CompletableFuture<Void> future = agent.stop();
assertThat(future).isDone();
assertThat(future).isCompletedExceptionally();
assertThatThrownBy(future::get).hasCauseInstanceOf(IllegalStateException.class).hasMessageContaining("Illegal attempt to stop");
// Stop after starting should succeed
startAgent();
final MockPeerConnection connection = (MockPeerConnection) agent.connect(createPeer()).get();
final CompletableFuture<Void> future2 = agent.stop();
assertThat(future2).isDone();
assertThat(future2).isNotCompletedExceptionally();
// Check peer was disconnected
assertThat(connection.getDisconnectReason()).contains(DisconnectReason.CLIENT_QUITTING);
// Cannot stop again
final CompletableFuture<Void> future3 = agent.stop();
assertThat(future3).isDone();
assertThat(future3).isCompletedExceptionally();
assertThatThrownBy(future3::get).hasCauseInstanceOf(IllegalStateException.class).hasMessageContaining("Illegal attempt to stop");
}
Aggregations