Search in sources :

Example 1 with RawMessage

use of org.hyperledger.besu.ethereum.p2p.rlpx.wire.RawMessage in project besu by hyperledger.

the class DeFramerTest method decode_handlesNoSharedCaps.

@Test
public void decode_handlesNoSharedCaps() {
    ChannelFuture future = NettyMocks.channelFuture(false);
    when(channel.closeFuture()).thenReturn(future);
    PeerInfo remotePeerInfo = new PeerInfo(p2pVersion, "bla", Arrays.asList(Capability.create("eth", 254)), 30303, Peer.randomId());
    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(IncompatiblePeerException.class);
    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) ArrayList(java.util.ArrayList) ByteBuf(io.netty.buffer.ByteBuf) RawMessage(org.hyperledger.besu.ethereum.p2p.rlpx.wire.RawMessage) Test(org.junit.Test)

Example 2 with RawMessage

use of org.hyperledger.besu.ethereum.p2p.rlpx.wire.RawMessage 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)

Example 3 with RawMessage

use of org.hyperledger.besu.ethereum.p2p.rlpx.wire.RawMessage 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 4 with RawMessage

use of org.hyperledger.besu.ethereum.p2p.rlpx.wire.RawMessage 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 5 with RawMessage

use of org.hyperledger.besu.ethereum.p2p.rlpx.wire.RawMessage in project besu by hyperledger.

the class DeFramerTest method decode_shouldHandleImmediateDisconnectMessage.

@Test
public void decode_shouldHandleImmediateDisconnectMessage() {
    DisconnectMessage disconnectMessage = DisconnectMessage.create(DisconnectReason.TOO_MANY_PEERS);
    ByteBuf disconnectData = Unpooled.wrappedBuffer(disconnectMessage.getData().toArray());
    when(framer.deframe(eq(disconnectData))).thenReturn(new RawMessage(disconnectMessage.getCode(), disconnectMessage.getData())).thenReturn(null);
    List<Object> out = new ArrayList<>();
    deFramer.decode(ctx, disconnectData, out);
    assertThat(connectFuture).isDone();
    assertThatThrownBy(connectFuture::get).hasCauseInstanceOf(PeerDisconnectedException.class).hasMessageContaining(disconnectMessage.getReason().toString());
    verify(ctx).close();
    assertThat(out).isEmpty();
}
Also used : DisconnectMessage(org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.DisconnectMessage) ArrayList(java.util.ArrayList) ByteBuf(io.netty.buffer.ByteBuf) RawMessage(org.hyperledger.besu.ethereum.p2p.rlpx.wire.RawMessage) PeerDisconnectedException(org.hyperledger.besu.ethereum.p2p.network.exceptions.PeerDisconnectedException) Test(org.junit.Test)

Aggregations

RawMessage (org.hyperledger.besu.ethereum.p2p.rlpx.wire.RawMessage)56 Test (org.junit.Test)49 MessageData (org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData)42 ArrayList (java.util.ArrayList)27 Bytes (org.apache.tuweni.bytes.Bytes)14 PeerConnection (org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection)13 ByteBuf (io.netty.buffer.ByteBuf)8 Hash (org.hyperledger.besu.datatypes.Hash)7 BlockDataGenerator (org.hyperledger.besu.ethereum.core.BlockDataGenerator)7 BytesValueRLPOutput (org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput)7 ChannelFuture (io.netty.channel.ChannelFuture)6 List (java.util.List)6 Capability (org.hyperledger.besu.ethereum.p2p.rlpx.wire.Capability)6 Collections (java.util.Collections)5 HashSet (java.util.HashSet)5 Set (java.util.Set)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 AtomicLong (java.util.concurrent.atomic.AtomicLong)5 Consumer (java.util.function.Consumer)5 DefaultPeer (org.hyperledger.besu.ethereum.p2p.peers.DefaultPeer)5