Search in sources :

Example 1 with Eth2RpcMethod

use of tech.pegasys.teku.networking.eth2.rpc.core.methods.Eth2RpcMethod in project teku by ConsenSys.

the class BeaconChainMethods method createMetadata.

private static Eth2RpcMethod<EmptyMessage, MetadataMessage> createMetadata(final Spec spec, final AsyncRunner asyncRunner, final MetadataMessagesFactory metadataMessagesFactory, final PeerLookup peerLookup, final RpcEncoding rpcEncoding) {
    final MetadataMessageHandler messageHandler = new MetadataMessageHandler(spec, metadataMessagesFactory);
    final EmptyMessageSchema requestType = EmptyMessage.SSZ_SCHEMA;
    final boolean expectResponse = true;
    final SszSchema<MetadataMessage> phase0MetadataSchema = SszSchema.as(MetadataMessage.class, spec.forMilestone(SpecMilestone.PHASE0).getSchemaDefinitions().getMetadataMessageSchema());
    final RpcContextCodec<?, MetadataMessage> phase0ContextCodec = RpcContextCodec.noop(phase0MetadataSchema);
    final SingleProtocolEth2RpcMethod<EmptyMessage, MetadataMessage> v1Method = new SingleProtocolEth2RpcMethod<>(asyncRunner, BeaconChainMethodIds.GET_METADATA, 1, rpcEncoding, requestType, expectResponse, phase0ContextCodec, messageHandler, peerLookup);
    if (spec.isMilestoneSupported(SpecMilestone.ALTAIR)) {
        final SszSchema<MetadataMessage> altairMetadataSchema = SszSchema.as(MetadataMessage.class, spec.forMilestone(SpecMilestone.ALTAIR).getSchemaDefinitions().getMetadataMessageSchema());
        final RpcContextCodec<?, MetadataMessage> altairContextCodec = RpcContextCodec.noop(altairMetadataSchema);
        final SingleProtocolEth2RpcMethod<EmptyMessage, MetadataMessage> v2Method = new SingleProtocolEth2RpcMethod<>(asyncRunner, BeaconChainMethodIds.GET_METADATA, 2, rpcEncoding, requestType, expectResponse, altairContextCodec, messageHandler, peerLookup);
        return VersionedEth2RpcMethod.create(rpcEncoding, requestType, expectResponse, List.of(v2Method, v1Method));
    } else {
        return v1Method;
    }
}
Also used : MetadataMessageHandler(tech.pegasys.teku.networking.eth2.rpc.beaconchain.methods.MetadataMessageHandler) SingleProtocolEth2RpcMethod(tech.pegasys.teku.networking.eth2.rpc.core.methods.SingleProtocolEth2RpcMethod) MetadataMessage(tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.metadata.MetadataMessage) EmptyMessageSchema(tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.EmptyMessage.EmptyMessageSchema) EmptyMessage(tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.EmptyMessage)

Example 2 with Eth2RpcMethod

use of tech.pegasys.teku.networking.eth2.rpc.core.methods.Eth2RpcMethod in project teku by ConsenSys.

the class BeaconChainMethods method createBeaconBlocksByRange.

private static Eth2RpcMethod<BeaconBlocksByRangeRequestMessage, SignedBeaconBlock> createBeaconBlocksByRange(final Spec spec, final AsyncRunner asyncRunner, final RecentChainData recentChainData, final CombinedChainDataClient combinedChainDataClient, final PeerLookup peerLookup, final RpcEncoding rpcEncoding) {
    final BeaconBlocksByRangeMessageHandler beaconBlocksByRangeHandler = new BeaconBlocksByRangeMessageHandler(spec, combinedChainDataClient, MAX_BLOCK_BY_RANGE_REQUEST_SIZE);
    // V1 request only deal with Phase0 blocks
    final SignedBeaconBlockSchema phase0BlockSchema = spec.forMilestone(SpecMilestone.PHASE0).getSchemaDefinitions().getSignedBeaconBlockSchema();
    final RpcContextCodec<?, SignedBeaconBlock> noContextCodec = RpcContextCodec.noop(phase0BlockSchema);
    final BeaconBlocksByRangeRequestMessageSchema requestType = BeaconBlocksByRangeRequestMessage.SSZ_SCHEMA;
    final boolean expectResponseToRequest = true;
    final SingleProtocolEth2RpcMethod<BeaconBlocksByRangeRequestMessage, SignedBeaconBlock> v1Method = new SingleProtocolEth2RpcMethod<>(asyncRunner, BeaconChainMethodIds.BEACON_BLOCKS_BY_RANGE, 1, rpcEncoding, requestType, expectResponseToRequest, noContextCodec, beaconBlocksByRangeHandler, peerLookup);
    if (spec.isMilestoneSupported(SpecMilestone.ALTAIR)) {
        final RpcContextCodec<Bytes4, SignedBeaconBlock> forkDigestContextCodec = RpcContextCodec.forkDigest(spec, recentChainData, ForkDigestPayloadContext.SIGNED_BEACONBLOCK);
        final SingleProtocolEth2RpcMethod<BeaconBlocksByRangeRequestMessage, SignedBeaconBlock> v2Method = new SingleProtocolEth2RpcMethod<>(asyncRunner, BeaconChainMethodIds.BEACON_BLOCKS_BY_RANGE, 2, rpcEncoding, requestType, expectResponseToRequest, forkDigestContextCodec, beaconBlocksByRangeHandler, peerLookup);
        return VersionedEth2RpcMethod.create(rpcEncoding, requestType, expectResponseToRequest, List.of(v2Method, v1Method));
    } else {
        return v1Method;
    }
}
Also used : SingleProtocolEth2RpcMethod(tech.pegasys.teku.networking.eth2.rpc.core.methods.SingleProtocolEth2RpcMethod) BeaconBlocksByRangeRequestMessageSchema(tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.BeaconBlocksByRangeRequestMessage.BeaconBlocksByRangeRequestMessageSchema) Bytes4(tech.pegasys.teku.infrastructure.bytes.Bytes4) BeaconBlocksByRangeMessageHandler(tech.pegasys.teku.networking.eth2.rpc.beaconchain.methods.BeaconBlocksByRangeMessageHandler) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) SignedBeaconBlockSchema(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlockSchema) BeaconBlocksByRangeRequestMessage(tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.BeaconBlocksByRangeRequestMessage)

Example 3 with Eth2RpcMethod

use of tech.pegasys.teku.networking.eth2.rpc.core.methods.Eth2RpcMethod 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 4 with Eth2RpcMethod

use of tech.pegasys.teku.networking.eth2.rpc.core.methods.Eth2RpcMethod in project teku by ConsenSys.

the class ErrorConditionsIntegrationTest method shouldRejectInvalidRequests.

@Test
public void shouldRejectInvalidRequests() throws Exception {
    final Eth2P2PNetwork network1 = networkFactory.builder().rpcEncoding(rpcEncoding).startNetwork();
    final Eth2P2PNetwork network2 = networkFactory.builder().rpcEncoding(rpcEncoding).peer(network1).startNetwork();
    final Eth2Peer peer = network1.getPeer(network2.getNodeId()).orElseThrow();
    final Eth2RpcMethod<StatusMessage, StatusMessage> status = ((ActiveEth2P2PNetwork) network1).getBeaconChainMethods().status();
    final SafeFuture<StatusMessage> response = peer.requestSingleItem(status, new InvalidStatusMessage(spec.getGenesisSpecConfig().getGenesisForkVersion()));
    final RpcException expected = new LengthOutOfBoundsException();
    Assertions.assertThatThrownBy(() -> Waiter.waitFor(response)).isInstanceOf(ExecutionException.class).extracting(Throwable::getCause).isInstanceOf(RpcException.class).is(new Condition<>(error -> {
        final RpcException rpcException = (RpcException) error;
        return rpcException.getErrorMessageString().equals(expected.getErrorMessageString()) && rpcException.getResponseCode() == expected.getResponseCode();
    }, "Exception did not match expected exception %s", expected));
}
Also used : LengthOutOfBoundsException(tech.pegasys.teku.networking.eth2.rpc.core.RpcException.LengthOutOfBoundsException) RpcException(tech.pegasys.teku.networking.eth2.rpc.core.RpcException) StatusMessage(tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.StatusMessage) SafeFuture(tech.pegasys.teku.infrastructure.async.SafeFuture) Eth2RpcMethod(tech.pegasys.teku.networking.eth2.rpc.core.methods.Eth2RpcMethod) Bytes(org.apache.tuweni.bytes.Bytes) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) TestSpecFactory(tech.pegasys.teku.spec.TestSpecFactory) RpcEncoding(tech.pegasys.teku.networking.eth2.rpc.core.encodings.RpcEncoding) Eth2Peer(tech.pegasys.teku.networking.eth2.peers.Eth2Peer) Condition(org.assertj.core.api.Condition) Waiter(tech.pegasys.teku.infrastructure.async.Waiter) Assertions(org.assertj.core.api.Assertions) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) Bytes4(tech.pegasys.teku.infrastructure.bytes.Bytes4) Spec(tech.pegasys.teku.spec.Spec) Bytes32(org.apache.tuweni.bytes.Bytes32) MAX_CHUNK_SIZE(tech.pegasys.teku.spec.config.Constants.MAX_CHUNK_SIZE) RpcException(tech.pegasys.teku.networking.eth2.rpc.core.RpcException) LengthOutOfBoundsException(tech.pegasys.teku.networking.eth2.rpc.core.RpcException.LengthOutOfBoundsException) Eth2Peer(tech.pegasys.teku.networking.eth2.peers.Eth2Peer) ExecutionException(java.util.concurrent.ExecutionException) StatusMessage(tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.StatusMessage) Test(org.junit.jupiter.api.Test)

Example 5 with Eth2RpcMethod

use of tech.pegasys.teku.networking.eth2.rpc.core.methods.Eth2RpcMethod in project teku by ConsenSys.

the class BeaconChainMethods method createGoodBye.

private static Eth2RpcMethod<GoodbyeMessage, GoodbyeMessage> createGoodBye(final AsyncRunner asyncRunner, final MetricsSystem metricsSystem, final PeerLookup peerLookup, final RpcEncoding rpcEncoding) {
    final GoodbyeMessageHandler goodbyeHandler = new GoodbyeMessageHandler(metricsSystem);
    final RpcContextCodec<?, GoodbyeMessage> contextCodec = RpcContextCodec.noop(GoodbyeMessage.SSZ_SCHEMA);
    return new SingleProtocolEth2RpcMethod<>(asyncRunner, BeaconChainMethodIds.GOODBYE, 1, rpcEncoding, GoodbyeMessage.SSZ_SCHEMA, false, contextCodec, goodbyeHandler, peerLookup);
}
Also used : SingleProtocolEth2RpcMethod(tech.pegasys.teku.networking.eth2.rpc.core.methods.SingleProtocolEth2RpcMethod) GoodbyeMessageHandler(tech.pegasys.teku.networking.eth2.rpc.beaconchain.methods.GoodbyeMessageHandler) GoodbyeMessage(tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.GoodbyeMessage)

Aggregations

SingleProtocolEth2RpcMethod (tech.pegasys.teku.networking.eth2.rpc.core.methods.SingleProtocolEth2RpcMethod)6 Bytes4 (tech.pegasys.teku.infrastructure.bytes.Bytes4)3 Bytes (org.apache.tuweni.bytes.Bytes)2 SignedBeaconBlock (tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock)2 SignedBeaconBlockSchema (tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlockSchema)2 StatusMessage (tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.StatusMessage)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 Test (org.junit.jupiter.api.Test)1 SafeFuture (tech.pegasys.teku.infrastructure.async.SafeFuture)1 Waiter (tech.pegasys.teku.infrastructure.async.Waiter)1 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)1 Eth2Peer (tech.pegasys.teku.networking.eth2.peers.Eth2Peer)1 BeaconBlocksByRangeMessageHandler (tech.pegasys.teku.networking.eth2.rpc.beaconchain.methods.BeaconBlocksByRangeMessageHandler)1 BeaconBlocksByRootMessageHandler (tech.pegasys.teku.networking.eth2.rpc.beaconchain.methods.BeaconBlocksByRootMessageHandler)1 GoodbyeMessageHandler (tech.pegasys.teku.networking.eth2.rpc.beaconchain.methods.GoodbyeMessageHandler)1 MetadataMessageHandler (tech.pegasys.teku.networking.eth2.rpc.beaconchain.methods.MetadataMessageHandler)1