Search in sources :

Example 1 with StatusMessage

use of tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.StatusMessage in project teku by ConsenSys.

the class AbstractSyncTest method withPeerHeadSlot.

protected PeerStatus withPeerHeadSlot(final UInt64 peerHeadSlot, final UInt64 peerFinalizedEpoch, final Bytes32 peerHeadBlockRoot) {
    final PeerStatus peer_status = PeerStatus.fromStatusMessage(new StatusMessage(Bytes4.leftPad(Bytes.EMPTY), Bytes32.ZERO, peerFinalizedEpoch, peerHeadBlockRoot, peerHeadSlot));
    when(peer.getStatus()).thenReturn(peer_status);
    return peer_status;
}
Also used : PeerStatus(tech.pegasys.teku.networking.eth2.peers.PeerStatus) StatusMessage(tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.StatusMessage)

Example 2 with StatusMessage

use of tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.StatusMessage in project teku by ConsenSys.

the class BeaconChainMethods method createStatus.

private static Eth2RpcMethod<StatusMessage, StatusMessage> createStatus(final AsyncRunner asyncRunner, final StatusMessageFactory statusMessageFactory, final PeerLookup peerLookup, final RpcEncoding rpcEncoding) {
    final StatusMessageHandler statusHandler = new StatusMessageHandler(statusMessageFactory);
    final RpcContextCodec<?, StatusMessage> contextCodec = RpcContextCodec.noop(StatusMessage.SSZ_SCHEMA);
    return new SingleProtocolEth2RpcMethod<>(asyncRunner, BeaconChainMethodIds.STATUS, 1, rpcEncoding, StatusMessage.SSZ_SCHEMA, true, contextCodec, statusHandler, peerLookup);
}
Also used : SingleProtocolEth2RpcMethod(tech.pegasys.teku.networking.eth2.rpc.core.methods.SingleProtocolEth2RpcMethod) StatusMessageHandler(tech.pegasys.teku.networking.eth2.rpc.beaconchain.methods.StatusMessageHandler) StatusMessage(tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.StatusMessage)

Example 3 with StatusMessage

use of tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.StatusMessage in project teku by ConsenSys.

the class LengthPrefixedEncodingTest method decodePayload_shouldReturnErrorWhenNoPayloadIsPresent.

@Test
public void decodePayload_shouldReturnErrorWhenNoPayloadIsPresent() {
    final Bytes statusMessageLengthPrefix = Bytes.fromHexString("0x54");
    List<List<ByteBuf>> testByteBufSlices = Utils.generateTestSlices(statusMessageLengthPrefix);
    for (Iterable<ByteBuf> bufSlices : testByteBufSlices) {
        RpcByteBufDecoder<StatusMessage> decoder = encoding.createDecoder(StatusMessage.SSZ_SCHEMA);
        List<ByteBuf> usedBufs = new ArrayList<>();
        assertThatThrownBy(() -> {
            for (ByteBuf bufSlice : bufSlices) {
                assertThat(decoder.decodeOneMessage(bufSlice)).isEmpty();
                bufSlice.release();
                usedBufs.add(bufSlice);
            }
            decoder.complete();
        }).isInstanceOf(PayloadTruncatedException.class);
        assertThat(usedBufs).allMatch(b -> b.refCnt() == 0);
    }
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) List(java.util.List) ByteBuf(io.netty.buffer.ByteBuf) StatusMessage(tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.StatusMessage) Test(org.junit.jupiter.api.Test)

Example 4 with StatusMessage

use of tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.StatusMessage in project teku by ConsenSys.

the class LengthPrefixedEncodingTest method decodePayload_shouldReadPayloadWhenExtraDataIsAppended.

@Test
public void decodePayload_shouldReadPayloadWhenExtraDataIsAppended() throws RpcException {
    final StatusMessage originalMessage = StatusMessage.createPreGenesisStatus(spec);
    final Bytes encoded = encoding.encodePayload(originalMessage);
    final Bytes extraData = Bytes.of(1, 2, 3, 4);
    List<List<ByteBuf>> testByteBufSlices = Utils.generateTestSlices(encoded, extraData);
    for (Iterable<ByteBuf> bufSlices : testByteBufSlices) {
        RpcByteBufDecoder<StatusMessage> decoder = encoding.createDecoder(StatusMessage.SSZ_SCHEMA);
        Optional<StatusMessage> result = Optional.empty();
        int unreadBytes = 0;
        for (ByteBuf bufSlice : bufSlices) {
            if (result.isEmpty()) {
                result = decoder.decodeOneMessage(bufSlice);
            }
            if (result.isPresent()) {
                unreadBytes += bufSlice.readableBytes();
            }
            bufSlice.release();
        }
        decoder.complete();
        assertThat(result).contains(originalMessage);
        assertThat(unreadBytes).isEqualTo(4);
        assertThat(bufSlices).allMatch(b -> b.refCnt() == 0);
    }
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) List(java.util.List) ByteBuf(io.netty.buffer.ByteBuf) StatusMessage(tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.StatusMessage) Test(org.junit.jupiter.api.Test)

Example 5 with StatusMessage

use of tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.StatusMessage in project teku by ConsenSys.

the class LengthPrefixedEncodingTest method decodePayload_shouldThrowRpcExceptionIfMessageLengthPrefixIsMoreThanThreeBytes.

@Test
public void decodePayload_shouldThrowRpcExceptionIfMessageLengthPrefixIsMoreThanThreeBytes() {
    final ByteBuf input = inputByteBuffer("0x80808001");
    RpcByteBufDecoder<StatusMessage> decoder = encoding.createDecoder(StatusMessage.SSZ_SCHEMA);
    assertThatThrownBy(() -> decoder.decodeOneMessage(input)).isInstanceOf(ChunkTooLongException.class);
    input.release();
    assertThat(input.refCnt()).isEqualTo(0);
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) StatusMessage(tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.StatusMessage) Test(org.junit.jupiter.api.Test)

Aggregations

StatusMessage (tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.StatusMessage)13 Test (org.junit.jupiter.api.Test)8 Bytes (org.apache.tuweni.bytes.Bytes)6 ByteBuf (io.netty.buffer.ByteBuf)5 ArrayList (java.util.ArrayList)3 Collections.singletonList (java.util.Collections.singletonList)3 List (java.util.List)3 PeerStatus (tech.pegasys.teku.networking.eth2.peers.PeerStatus)3 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)2 ExecutionException (java.util.concurrent.ExecutionException)1 Bytes32 (org.apache.tuweni.bytes.Bytes32)1 Assertions (org.assertj.core.api.Assertions)1 Condition (org.assertj.core.api.Condition)1 AfterEach (org.junit.jupiter.api.AfterEach)1 SafeFuture (tech.pegasys.teku.infrastructure.async.SafeFuture)1 Waiter (tech.pegasys.teku.infrastructure.async.Waiter)1 Bytes4 (tech.pegasys.teku.infrastructure.bytes.Bytes4)1 Eth2Peer (tech.pegasys.teku.networking.eth2.peers.Eth2Peer)1 StatusMessageHandler (tech.pegasys.teku.networking.eth2.rpc.beaconchain.methods.StatusMessageHandler)1 RpcException (tech.pegasys.teku.networking.eth2.rpc.core.RpcException)1