Search in sources :

Example 26 with Peer

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);
    }
}
Also used : MockPeerConnection(org.hyperledger.besu.ethereum.p2p.rlpx.connections.MockPeerConnection) PeerConnection(org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection) Peer(org.hyperledger.besu.ethereum.p2p.peers.Peer) PeerTestHelper.createPeer(org.hyperledger.besu.ethereum.p2p.peers.PeerTestHelper.createPeer) DefaultPeer(org.hyperledger.besu.ethereum.p2p.peers.DefaultPeer) DiscoveryPeer(org.hyperledger.besu.ethereum.p2p.discovery.DiscoveryPeer) Test(org.junit.Test)

Example 27 with Peer

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);
}
Also used : MockPeerConnection(org.hyperledger.besu.ethereum.p2p.rlpx.connections.MockPeerConnection) Peer(org.hyperledger.besu.ethereum.p2p.peers.Peer) PeerTestHelper.createPeer(org.hyperledger.besu.ethereum.p2p.peers.PeerTestHelper.createPeer) DefaultPeer(org.hyperledger.besu.ethereum.p2p.peers.DefaultPeer) DiscoveryPeer(org.hyperledger.besu.ethereum.p2p.discovery.DiscoveryPeer) Test(org.junit.Test)

Example 28 with Peer

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);
}
Also used : Peer(org.hyperledger.besu.ethereum.p2p.peers.Peer) PeerTestHelper.createPeer(org.hyperledger.besu.ethereum.p2p.peers.PeerTestHelper.createPeer) DefaultPeer(org.hyperledger.besu.ethereum.p2p.peers.DefaultPeer) DiscoveryPeer(org.hyperledger.besu.ethereum.p2p.discovery.DiscoveryPeer) Test(org.junit.Test)

Example 29 with Peer

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();
}
Also used : MockPeerConnection(org.hyperledger.besu.ethereum.p2p.rlpx.connections.MockPeerConnection) PeerConnection(org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection) Peer(org.hyperledger.besu.ethereum.p2p.peers.Peer) PeerTestHelper.createPeer(org.hyperledger.besu.ethereum.p2p.peers.PeerTestHelper.createPeer) DefaultPeer(org.hyperledger.besu.ethereum.p2p.peers.DefaultPeer) DiscoveryPeer(org.hyperledger.besu.ethereum.p2p.discovery.DiscoveryPeer) Test(org.junit.Test)

Example 30 with Peer

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);
}
Also used : MockPeerConnection(org.hyperledger.besu.ethereum.p2p.rlpx.connections.MockPeerConnection) Peer(org.hyperledger.besu.ethereum.p2p.peers.Peer) PeerTestHelper.createPeer(org.hyperledger.besu.ethereum.p2p.peers.PeerTestHelper.createPeer) DefaultPeer(org.hyperledger.besu.ethereum.p2p.peers.DefaultPeer) DiscoveryPeer(org.hyperledger.besu.ethereum.p2p.discovery.DiscoveryPeer) Test(org.junit.Test)

Aggregations

Peer (org.hyperledger.besu.ethereum.p2p.peers.Peer)108 Test (org.junit.Test)102 DefaultPeer (org.hyperledger.besu.ethereum.p2p.peers.DefaultPeer)73 PeerTestHelper.createPeer (org.hyperledger.besu.ethereum.p2p.peers.PeerTestHelper.createPeer)59 DiscoveryPeer (org.hyperledger.besu.ethereum.p2p.discovery.DiscoveryPeer)54 PeerConnection (org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection)51 MockPeerConnection (org.hyperledger.besu.ethereum.p2p.rlpx.connections.MockPeerConnection)41 CompletableFuture (java.util.concurrent.CompletableFuture)25 Bytes (org.apache.tuweni.bytes.Bytes)21 EnodeURL (org.hyperledger.besu.plugin.data.EnodeURL)16 DisconnectReason (org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.DisconnectMessage.DisconnectReason)14 PeerPermissions (org.hyperledger.besu.ethereum.p2p.permissions.PeerPermissions)10 MockPeerDiscoveryAgent (org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent)8 Optional (java.util.Optional)7 NodeKey (org.hyperledger.besu.crypto.NodeKey)7 IncomingPacket (org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent.IncomingPacket)7 PeerPermissionsDenylist (org.hyperledger.besu.ethereum.p2p.permissions.PeerPermissionsDenylist)6 PeerInfo (org.hyperledger.besu.ethereum.p2p.rlpx.wire.PeerInfo)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5