Search in sources :

Example 1 with PingMessage

use of org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.PingMessage 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 2 with PingMessage

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

the class DeFramerTest method decode_handlesHello.

@Test
public void decode_handlesHello() throws ExecutionException, InterruptedException {
    ChannelFuture future = NettyMocks.channelFuture(false);
    when(channel.closeFuture()).thenReturn(future);
    final Peer peer = createRemotePeer();
    final PeerInfo remotePeerInfo = createPeerInfo(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).isNotCompletedExceptionally();
    PeerConnection peerConnection = connectFuture.get();
    assertThat(peerConnection.getPeerInfo()).isEqualTo(remotePeerInfo);
    EnodeURL expectedEnode = EnodeURLImpl.builder().configureFromEnode(peer.getEnodeURL()).disableDiscovery().build();
    assertThat(peerConnection.getPeer().getEnodeURL()).isEqualTo(expectedEnode);
    assertThat(out).isEmpty();
    // 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) EnodeURL(org.hyperledger.besu.plugin.data.EnodeURL) 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) RawMessage(org.hyperledger.besu.ethereum.p2p.rlpx.wire.RawMessage) PingMessage(org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.PingMessage) Test(org.junit.Test)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)2 ChannelFuture (io.netty.channel.ChannelFuture)2 ArrayList (java.util.ArrayList)2 DefaultPeer (org.hyperledger.besu.ethereum.p2p.peers.DefaultPeer)2 Peer (org.hyperledger.besu.ethereum.p2p.peers.Peer)2 PeerConnection (org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection)2 PeerInfo (org.hyperledger.besu.ethereum.p2p.rlpx.wire.PeerInfo)2 RawMessage (org.hyperledger.besu.ethereum.p2p.rlpx.wire.RawMessage)2 HelloMessage (org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.HelloMessage)2 PingMessage (org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.PingMessage)2 EnodeURL (org.hyperledger.besu.plugin.data.EnodeURL)2 Test (org.junit.Test)2