Search in sources :

Example 1 with Peer

use of org.hyperledger.besu.ethereum.p2p.peers.Peer 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);
    }
}
Also used : EnodeURL(org.hyperledger.besu.plugin.data.EnodeURL) Bytes(org.apache.tuweni.bytes.Bytes) PeerPermissionsDenylist(org.hyperledger.besu.ethereum.p2p.permissions.PeerPermissionsDenylist) CompletableFuture(java.util.concurrent.CompletableFuture) PeerConnection(org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection) DisconnectReason(org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.DisconnectMessage.DisconnectReason) Peer(org.hyperledger.besu.ethereum.p2p.peers.Peer) DefaultPeer(org.hyperledger.besu.ethereum.p2p.peers.DefaultPeer) Test(org.junit.Test)

Example 2 with Peer

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

the class PeerReputationManagerTest method generatePeerConnection.

private PeerConnection generatePeerConnection() {
    final Bytes nodeId = Peer.randomId();
    final PeerConnection conn = mock(PeerConnection.class);
    final PeerInfo peerInfo = mock(PeerInfo.class);
    final Peer peer = generatePeer();
    when(peerInfo.getNodeId()).thenReturn(nodeId);
    when(conn.getPeerInfo()).thenReturn(peerInfo);
    when(conn.getPeer()).thenReturn(peer);
    return conn;
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) PeerConnection(org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection) 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)

Example 3 with Peer

use of org.hyperledger.besu.ethereum.p2p.peers.Peer 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);
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) DisconnectReason(org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.DisconnectMessage.DisconnectReason) PeerTestHelper.createPeer(org.hyperledger.besu.ethereum.p2p.peers.PeerTestHelper.createPeer) Peer(org.hyperledger.besu.ethereum.p2p.peers.Peer) Test(org.junit.Test)

Example 4 with Peer

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

the class RlpxConnectionTest method getPeer_pendingOutboundConnection.

@Test
public void getPeer_pendingOutboundConnection() {
    final Peer peer = createPeer();
    final CompletableFuture<PeerConnection> future = new CompletableFuture<>();
    final RlpxConnection conn = RlpxConnection.outboundConnection(peer, future);
    assertThat(conn.getPeer()).isEqualTo(peer);
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) PeerTestHelper.createPeer(org.hyperledger.besu.ethereum.p2p.peers.PeerTestHelper.createPeer) Peer(org.hyperledger.besu.ethereum.p2p.peers.Peer) Test(org.junit.Test)

Example 5 with Peer

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

the class DeFramerTest method decode_handlesHelloFromPeerWithAdvertisedPortOf0.

@Test
public void decode_handlesHelloFromPeerWithAdvertisedPortOf0() throws ExecutionException, InterruptedException {
    ChannelFuture future = NettyMocks.channelFuture(false);
    when(channel.closeFuture()).thenReturn(future);
    final Peer peer = createRemotePeer();
    final PeerInfo remotePeerInfo = new PeerInfo(p2pVersion, clientId, capabilities, 0, peer.getId());
    final DeFramer deFramer = createDeFramer(null);
    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).isNotCompletedExceptionally();
    PeerConnection peerConnection = connectFuture.get();
    assertThat(peerConnection.getPeerInfo()).isEqualTo(remotePeerInfo);
    assertThat(out).isEmpty();
    final EnodeURL expectedEnode = EnodeURLImpl.builder().ipAddress(remoteAddress.getAddress()).nodeId(peer.getId()).disableListening().disableDiscovery().build();
    assertThat(peerConnection.getPeer().getEnodeURL()).isEqualTo(expectedEnode);
    // Next phase of pipeline should be setup
    verify(pipeline, times(1)).addLast(any());
    // Next message should be pushed out
    PingMessage nextMessage = PingMessage.get();
    ByteBuf nextData = Unpooled.wrappedBuffer(nextMessage.getData().toArray());
    when(framer.deframe(eq(nextData))).thenReturn(new RawMessage(nextMessage.getCode(), nextMessage.getData())).thenReturn(null);
    verify(pipeline, times(1)).addLast(any());
    deFramer.decode(ctx, nextData, out);
    assertThat(out.size()).isEqualTo(1);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) HelloMessage(org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.HelloMessage) PeerConnection(org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection) 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) PingMessage(org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.PingMessage) EnodeURL(org.hyperledger.besu.plugin.data.EnodeURL) RawMessage(org.hyperledger.besu.ethereum.p2p.rlpx.wire.RawMessage) 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