Search in sources :

Example 1 with BeaconBlocksByRootRequestMessage

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

the class RpcRequestDecoderTest method shouldParseSingleResponse.

@Test
public void shouldParseSingleResponse() throws Exception {
    List<List<ByteBuf>> testByteBufSlices = testByteBufSlices(LENGTH_PREFIX, MESSAGE_DATA);
    for (Iterable<ByteBuf> bufSlices : testByteBufSlices) {
        RpcRequestDecoder<BeaconBlocksByRootRequestMessage> decoder = METHOD.createRequestDecoder();
        List<BeaconBlocksByRootRequestMessage> msgs = new ArrayList<>();
        for (ByteBuf bufSlice : bufSlices) {
            decoder.decodeRequest(bufSlice).ifPresent(msgs::add);
            bufSlice.release();
        }
        assertThat(decoder.complete()).isEmpty();
        for (ByteBuf bufSlice : bufSlices) {
            assertThat(bufSlice.refCnt()).isEqualTo(0);
        }
        assertThat(msgs).containsExactly(MESSAGE);
    }
}
Also used : BeaconBlocksByRootRequestMessage(tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.BeaconBlocksByRootRequestMessage) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 2 with BeaconBlocksByRootRequestMessage

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

the class RpcResponseDecoderTest method decodeNextResponse_shouldParseMultipleResponses.

@Test
public void decodeNextResponse_shouldParseMultipleResponses() throws Exception {
    final BeaconBlocksByRootRequestMessage secondMessage = createRequestMessage(2);
    final Bytes secondMessageData = secondMessage.sszSerialize();
    final Bytes compressedPayload = COMPRESSOR.compress(secondMessageData);
    for (Iterable<ByteBuf> testByteBufSlice : testByteBufSlices(SUCCESS_CODE, LENGTH_PREFIX, MESSAGE_DATA, SUCCESS_CODE, getLengthPrefix(secondMessageData.size()), compressedPayload)) {
        List<BeaconBlocksByRootRequestMessage> results = new ArrayList<>();
        for (ByteBuf byteBuf : testByteBufSlice) {
            results.addAll(decoder.decodeNextResponses(byteBuf));
            byteBuf.release();
        }
        decoder.complete();
        assertThat(results).containsExactly(MESSAGE, secondMessage);
        assertThat(testByteBufSlice).allSatisfy(b -> assertThat(b.refCnt()).isEqualTo(0));
    }
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) BeaconBlocksByRootRequestMessage(tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.BeaconBlocksByRootRequestMessage) ArrayList(java.util.ArrayList) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 3 with BeaconBlocksByRootRequestMessage

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

the class RpcResponseDecoderTest method decodeNextResponse_shouldParseSingleResponse.

@Test
public void decodeNextResponse_shouldParseSingleResponse() throws Exception {
    for (Iterable<ByteBuf> testByteBufSlice : testByteBufSlices(SUCCESS_CODE, LENGTH_PREFIX, MESSAGE_DATA)) {
        List<BeaconBlocksByRootRequestMessage> results = new ArrayList<>();
        for (ByteBuf byteBuf : testByteBufSlice) {
            results.addAll(decoder.decodeNextResponses(byteBuf));
            byteBuf.release();
        }
        decoder.complete();
        assertThat(results).containsExactly(MESSAGE);
        assertThat(testByteBufSlice).allSatisfy(b -> assertThat(b.refCnt()).isEqualTo(0));
    }
}
Also used : BeaconBlocksByRootRequestMessage(tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.BeaconBlocksByRootRequestMessage) ArrayList(java.util.ArrayList) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 4 with BeaconBlocksByRootRequestMessage

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

the class LengthPrefixedEncodingTest method encodePayload_shouldEncodeBlocksByRootRequest.

@Test
public void encodePayload_shouldEncodeBlocksByRootRequest() {
    final Bytes encoded = encoding.encodePayload(new BeaconBlocksByRootRequestMessage(singletonList(Bytes32.ZERO)));
    // Just the length prefix and the hash itself.
    assertThat(encoded).isEqualTo(Bytes.wrap(Bytes.fromHexString("0x20"), new SnappyFramedCompressor().compress(Bytes32.ZERO)));
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) BeaconBlocksByRootRequestMessage(tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.BeaconBlocksByRootRequestMessage) SnappyFramedCompressor(tech.pegasys.teku.networking.eth2.rpc.core.encodings.compression.snappy.SnappyFramedCompressor) Test(org.junit.jupiter.api.Test)

Example 5 with BeaconBlocksByRootRequestMessage

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

the class BeaconBlocksByRootMessageHandlerTest method onIncomingMessage_interruptedByClosedStream.

@ParameterizedTest(name = "protocol={0}")
@MethodSource("protocolIdParams")
public void onIncomingMessage_interruptedByClosedStream(final String protocolId) {
    final List<SignedBeaconBlock> blocks = buildChain(5);
    // Mock callback to appear to be closed
    doThrow(new StreamClosedException()).when(callback).respond(any());
    final BeaconBlocksByRootRequestMessage message = createRequest(blocks);
    handler.onIncomingMessage(protocolId, peer, message, callback);
    // Check that we only asked for the first block
    verify(store, times(1)).retrieveSignedBlock(any());
    verify(callback, times(1)).respond(any());
}
Also used : BeaconBlocksByRootRequestMessage(tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.BeaconBlocksByRootRequestMessage) StreamClosedException(tech.pegasys.teku.networking.p2p.rpc.StreamClosedException) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

BeaconBlocksByRootRequestMessage (tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.BeaconBlocksByRootRequestMessage)11 Test (org.junit.jupiter.api.Test)7 SignedBeaconBlock (tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock)6 ArrayList (java.util.ArrayList)5 ByteBuf (io.netty.buffer.ByteBuf)4 Bytes (org.apache.tuweni.bytes.Bytes)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 List (java.util.List)2 MethodSource (org.junit.jupiter.params.provider.MethodSource)2 RpcException (tech.pegasys.teku.networking.eth2.rpc.core.RpcException)2 SnappyFramedCompressor (tech.pegasys.teku.networking.eth2.rpc.core.encodings.compression.snappy.SnappyFramedCompressor)2 StreamClosedException (tech.pegasys.teku.networking.p2p.rpc.StreamClosedException)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Throwables (com.google.common.base.Throwables)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 Collections.singletonList (java.util.Collections.singletonList)1 Optional (java.util.Optional)1 LogManager (org.apache.logging.log4j.LogManager)1 SafeFuture (tech.pegasys.teku.infrastructure.async.SafeFuture)1 Bytes4 (tech.pegasys.teku.infrastructure.bytes.Bytes4)1