Search in sources :

Example 6 with Peer

use of org.hyperledger.besu.ethereum.p2p.peers.Peer in project besu by hyperledger.

the class DeFramerTest method decode_shouldHandleRemoteSocketAddressIsNull.

@Test
public void decode_shouldHandleRemoteSocketAddressIsNull() {
    final Peer peer = createRemotePeer();
    final PeerInfo remotePeerInfo = new PeerInfo(p2pVersion, clientId, capabilities, 0, peer.getId());
    HelloMessage helloMessage = HelloMessage.create(remotePeerInfo);
    ByteBuf data = Unpooled.wrappedBuffer(helloMessage.getData().toArray());
    when(framer.deframe(any())).thenReturn(new RawMessage(helloMessage.getCode(), helloMessage.getData())).thenReturn(null);
    when(ctx.channel().remoteAddress()).thenReturn(null);
    ChannelFuture future = NettyMocks.channelFuture(true);
    when(ctx.writeAndFlush(any())).thenReturn(future);
    List<Object> out = new ArrayList<>();
    deFramer.decode(ctx, data, out);
    assertThat(connectFuture).isDone();
    assertThat(connectFuture).isCompletedExceptionally();
    assertThatThrownBy(connectFuture::get).hasCauseInstanceOf(PeerChannelClosedException.class);
    verify(ctx).close();
    assertThat(out).isEmpty();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) HelloMessage(org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.HelloMessage) PeerInfo(org.hyperledger.besu.ethereum.p2p.rlpx.wire.PeerInfo) Peer(org.hyperledger.besu.ethereum.p2p.peers.Peer) DefaultPeer(org.hyperledger.besu.ethereum.p2p.peers.DefaultPeer) ArrayList(java.util.ArrayList) ByteBuf(io.netty.buffer.ByteBuf) RawMessage(org.hyperledger.besu.ethereum.p2p.rlpx.wire.RawMessage) Test(org.junit.Test)

Example 7 with Peer

use of org.hyperledger.besu.ethereum.p2p.peers.Peer in project besu by hyperledger.

the class DeFramerTest method decode_handlesUnexpectedPeerId.

@Test
public void decode_handlesUnexpectedPeerId() {
    ChannelFuture future = NettyMocks.channelFuture(false);
    when(channel.closeFuture()).thenReturn(future);
    final Peer peer = createRemotePeer();
    final Bytes mismatchedId = Peer.randomId();
    final PeerInfo remotePeerInfo = new PeerInfo(p2pVersion, clientId, capabilities, peer.getEnodeURL().getListeningPortOrZero(), mismatchedId);
    final DeFramer deFramer = createDeFramer(peer);
    HelloMessage helloMessage = HelloMessage.create(remotePeerInfo);
    ByteBuf data = Unpooled.wrappedBuffer(helloMessage.getData().toArray());
    when(framer.deframe(eq(data))).thenReturn(new RawMessage(helloMessage.getCode(), helloMessage.getData())).thenReturn(null);
    List<Object> out = new ArrayList<>();
    deFramer.decode(ctx, data, out);
    assertThat(connectFuture).isDone();
    assertThat(connectFuture).isCompletedExceptionally();
    assertThatThrownBy(connectFuture::get).hasCauseInstanceOf(UnexpectedPeerConnectionException.class).hasMessageContaining("Expected id " + peer.getId().toString());
    assertThat(out).isEmpty();
    // Next phase of pipeline should be setup
    verify(pipeline, times(1)).addLast(any());
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) HelloMessage(org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.HelloMessage) PeerInfo(org.hyperledger.besu.ethereum.p2p.rlpx.wire.PeerInfo) Peer(org.hyperledger.besu.ethereum.p2p.peers.Peer) DefaultPeer(org.hyperledger.besu.ethereum.p2p.peers.DefaultPeer) ArrayList(java.util.ArrayList) ByteBuf(io.netty.buffer.ByteBuf) Bytes(org.apache.tuweni.bytes.Bytes) UnexpectedPeerConnectionException(org.hyperledger.besu.ethereum.p2p.network.exceptions.UnexpectedPeerConnectionException) RawMessage(org.hyperledger.besu.ethereum.p2p.rlpx.wire.RawMessage) Test(org.junit.Test)

Example 8 with Peer

use of org.hyperledger.besu.ethereum.p2p.peers.Peer in project besu by hyperledger.

the class PeerPermissionsDenylistTest method remove_peer.

@Test
public void remove_peer() {
    PeerPermissionsDenylist denylist = PeerPermissionsDenylist.create();
    Peer peer = createPeer();
    denylist.add(peer);
    final AtomicInteger callbackCount = new AtomicInteger(0);
    denylist.subscribeUpdate((restricted, affectedPeers) -> {
        callbackCount.incrementAndGet();
        assertThat(restricted).isFalse();
        assertThat(affectedPeers).contains(Collections.singletonList(peer));
    });
    assertThat(callbackCount).hasValue(0);
    denylist.remove(peer);
    assertThat(callbackCount).hasValue(1);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DefaultPeer(org.hyperledger.besu.ethereum.p2p.peers.DefaultPeer) Peer(org.hyperledger.besu.ethereum.p2p.peers.Peer) Test(org.junit.Test)

Example 9 with Peer

use of org.hyperledger.besu.ethereum.p2p.peers.Peer in project besu by hyperledger.

the class PeerPermissionsDenylistTest method subscribeUpdate.

@Test
public void subscribeUpdate() {
    PeerPermissionsDenylist denylist = PeerPermissionsDenylist.create();
    final AtomicInteger callbackCount = new AtomicInteger(0);
    final AtomicInteger restrictedCallbackCount = new AtomicInteger(0);
    Peer peer = createPeer();
    denylist.subscribeUpdate((permissionsRestricted, affectedPeers) -> {
        callbackCount.incrementAndGet();
        if (permissionsRestricted) {
            restrictedCallbackCount.incrementAndGet();
        }
    });
    checkPermissions(denylist, peer, true);
    assertThat(callbackCount).hasValue(0);
    assertThat(restrictedCallbackCount).hasValue(0);
    denylist.add(peer);
    assertThat(callbackCount).hasValue(1);
    assertThat(restrictedCallbackCount).hasValue(1);
    denylist.add(peer);
    assertThat(callbackCount).hasValue(1);
    assertThat(restrictedCallbackCount).hasValue(1);
    denylist.remove(peer);
    assertThat(callbackCount).hasValue(2);
    assertThat(restrictedCallbackCount).hasValue(1);
    denylist.remove(peer);
    assertThat(callbackCount).hasValue(2);
    assertThat(restrictedCallbackCount).hasValue(1);
    denylist.add(peer);
    assertThat(callbackCount).hasValue(3);
    assertThat(restrictedCallbackCount).hasValue(2);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DefaultPeer(org.hyperledger.besu.ethereum.p2p.peers.DefaultPeer) Peer(org.hyperledger.besu.ethereum.p2p.peers.Peer) Test(org.junit.Test)

Example 10 with Peer

use of org.hyperledger.besu.ethereum.p2p.peers.Peer in project besu by hyperledger.

the class PeerPermissionsDenylistTest method createWithLimitedCapacity.

@Test
public void createWithLimitedCapacity() {
    final PeerPermissionsDenylist denylist = PeerPermissionsDenylist.create(2);
    Peer peerA = createPeer();
    Peer peerB = createPeer();
    Peer peerC = createPeer();
    // All peers are initially permitted
    checkPermissions(denylist, peerA, true);
    checkPermissions(denylist, peerB, true);
    checkPermissions(denylist, peerC, true);
    // Add peerA
    denylist.add(peerA);
    checkPermissions(denylist, peerA, false);
    checkPermissions(denylist, peerB, true);
    checkPermissions(denylist, peerC, true);
    // Add peerB
    denylist.add(peerB);
    checkPermissions(denylist, peerA, false);
    checkPermissions(denylist, peerB, false);
    checkPermissions(denylist, peerC, true);
    // Add peerC
    // Limit is exceeded and peerA should drop off of the list and be allowed
    denylist.add(peerC);
    checkPermissions(denylist, peerA, true);
    checkPermissions(denylist, peerB, false);
    checkPermissions(denylist, peerC, false);
}
Also used : DefaultPeer(org.hyperledger.besu.ethereum.p2p.peers.DefaultPeer) Peer(org.hyperledger.besu.ethereum.p2p.peers.Peer) 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