use of org.hyperledger.besu.ethereum.p2p.peers.Peer in project besu by hyperledger.
the class RlpxAgentTest method connect_withMaxRemotelyInitiatedConnectionsAtZeroPercent.
@Test
public void connect_withMaxRemotelyInitiatedConnectionsAtZeroPercent() {
final int maxPeers = 10;
final float maxRemotePeersFraction = 0.0f;
config.setLimitRemoteWireConnectionsEnabled(true);
config.setFractionRemoteWireConnectionsAllowed(maxRemotePeersFraction);
startAgentWithMaxPeers(maxPeers);
// Connect max local peers
for (int i = 0; i < maxPeers; i++) {
final Peer peer = createPeer();
final CompletableFuture<PeerConnection> connection = agent.connect(peer);
assertThat(connection).isDone();
assertThat(connection).isNotCompletedExceptionally();
assertThat(agent.getPeerConnection(peer)).contains(connection);
}
}
use of org.hyperledger.besu.ethereum.p2p.peers.Peer in project besu by hyperledger.
the class RlpxAgentTest method incomingConnection_withMaxRemotelyInitiatedConnectionsAtZeroPercent.
@Test
public void incomingConnection_withMaxRemotelyInitiatedConnectionsAtZeroPercent() {
final int maxPeers = 10;
final float maxRemotePeersFraction = 0.0f;
config.setLimitRemoteWireConnectionsEnabled(true);
config.setFractionRemoteWireConnectionsAllowed(maxRemotePeersFraction);
startAgentWithMaxPeers(maxPeers);
// First 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.peers.Peer in project besu by hyperledger.
the class RlpxAgentTest method getConnectionCount.
@Test
public void getConnectionCount() throws ExecutionException, InterruptedException {
startAgent();
final Peer peerA = createPeer();
final Peer peerB = createPeer();
final Peer peerC = createPeer();
assertThat(agent.getConnectionCount()).isEqualTo(0);
agent.connect(peerA);
assertThat(agent.getConnectionCount()).isEqualTo(1);
agent.connect(peerA);
assertThat(agent.getConnectionCount()).isEqualTo(1);
agent.connect(peerB);
assertThat(agent.getConnectionCount()).isEqualTo(2);
agent.connect(peerC);
assertThat(agent.getConnectionCount()).isEqualTo(3);
// Disconnect should decrement count
agent.disconnect(peerB.getId(), DisconnectReason.UNKNOWN);
assertThat(agent.getConnectionCount()).isEqualTo(2);
}
use of org.hyperledger.besu.ethereum.p2p.peers.Peer in project besu by hyperledger.
the class RlpxAgentTest method permissionsUpdate_permissionsRelaxedWithListOfPeers.
@Test
public void permissionsUpdate_permissionsRelaxedWithListOfPeers() throws ExecutionException, InterruptedException {
final Peer peerA = createPeer();
final Peer peerB = createPeer();
startAgent();
final PeerConnection connectionA = agent.connect(peerA).get();
final PeerConnection connectionB = agent.connect(peerB).get();
// Sanity check
assertThat(agent.getConnectionCount()).isEqualTo(2);
peerPermissions.testDispatchUpdate(false, Optional.of(Arrays.asList(peerA)));
assertThat(agent.getConnectionCount()).isEqualTo(2);
assertThat(agent.getPeerConnection(peerA)).isNotEmpty();
assertThat(agent.getPeerConnection(peerB)).isNotEmpty();
assertThat(connectionA.isDisconnected()).isFalse();
assertThat(connectionB.isDisconnected()).isFalse();
}
use of org.hyperledger.besu.ethereum.p2p.peers.Peer in project besu by hyperledger.
the class RlpxAgentTest method incomingConnection_succeedsForPrivilegedPeerWhenMaxRemoteConnectionsExceeded.
@Test
public void incomingConnection_succeedsForPrivilegedPeerWhenMaxRemoteConnectionsExceeded() {
final int maxPeers = 5;
final int maxRemotePeers = 3;
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();
}
// Sanity check
assertThat(agent.getConnectionCount()).isEqualTo(maxRemotePeers);
final Peer privilegedPeer = createPeer();
when(peerPrivileges.canExceedConnectionLimits(privilegedPeer)).thenReturn(true);
final MockPeerConnection privilegedConnection = connection(privilegedPeer);
connectionInitializer.simulateIncomingConnection(privilegedConnection);
assertThat(privilegedConnection.isDisconnected()).isFalse();
// No peers should be disconnected - exempt connections are ignored when enforcing this limit
assertThat(agent.getConnectionCount()).isEqualTo(maxRemotePeers + 1);
// The next non-exempt connection should fail
final Peer remotelyInitiatedPeer = createPeer();
final MockPeerConnection incomingConnection = connection(remotelyInitiatedPeer);
connectionInitializer.simulateIncomingConnection(incomingConnection);
assertThat(agent.getConnectionCount()).isEqualTo(maxRemotePeers + 1);
assertThat(incomingConnection.getDisconnectReason()).contains(DisconnectReason.TOO_MANY_PEERS);
}
Aggregations