Search in sources :

Example 1 with JsonProvider

use of tech.pegasys.teku.provider.JsonProvider in project teku by ConsenSys.

the class GetBlockHeaders method handle.

@OpenApi(path = ROUTE, method = HttpMethod.GET, summary = "Get block headers", tags = { TAG_BEACON }, description = "Retrieves block headers matching given query. By default it will fetch current head slot blocks.", queryParams = { @OpenApiParam(name = SLOT), @OpenApiParam(name = PARENT_ROOT, description = "Not currently supported.") }, responses = { @OpenApiResponse(status = RES_OK, content = @OpenApiContent(from = GetBlockHeadersResponse.class)), @OpenApiResponse(status = RES_BAD_REQUEST), @OpenApiResponse(status = RES_INTERNAL_ERROR) })
@Override
public void handle(@NotNull final Context ctx) throws Exception {
    final Map<String, List<String>> queryParameters = ctx.queryParamMap();
    final Optional<Bytes32> parentRoot = SingleQueryParameterUtils.getParameterValueAsBytes32IfPresent(queryParameters, PARENT_ROOT);
    final Optional<UInt64> slot = SingleQueryParameterUtils.getParameterValueAsUInt64IfPresent(queryParameters, SLOT);
    try {
        ctx.future(chainDataProvider.getBlockHeaders(parentRoot, slot).thenApplyChecked(jsonProvider::objectToJSON).exceptionallyCompose(error -> handleError(ctx, error)));
    } catch (final IllegalArgumentException e) {
        ctx.status(SC_BAD_REQUEST);
        ctx.json(jsonProvider.objectToJSON(new BadRequest(e.getMessage())));
    }
}
Also used : AbstractHandler(tech.pegasys.teku.beaconrestapi.handlers.AbstractHandler) SafeFuture(tech.pegasys.teku.infrastructure.async.SafeFuture) TAG_BEACON(tech.pegasys.teku.infrastructure.http.RestApiConstants.TAG_BEACON) OpenApiContent(io.javalin.plugin.openapi.annotations.OpenApiContent) SLOT(tech.pegasys.teku.infrastructure.http.RestApiConstants.SLOT) RES_INTERNAL_ERROR(tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_INTERNAL_ERROR) RES_BAD_REQUEST(tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_BAD_REQUEST) PARENT_ROOT(tech.pegasys.teku.infrastructure.http.RestApiConstants.PARENT_ROOT) Context(io.javalin.http.Context) OpenApiResponse(io.javalin.plugin.openapi.annotations.OpenApiResponse) Map(java.util.Map) JsonProvider(tech.pegasys.teku.provider.JsonProvider) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) Bytes32(org.apache.tuweni.bytes.Bytes32) ChainDataProvider(tech.pegasys.teku.api.ChainDataProvider) HttpMethod(io.javalin.plugin.openapi.annotations.HttpMethod) BadRequest(tech.pegasys.teku.beaconrestapi.schema.BadRequest) Throwables(com.google.common.base.Throwables) SingleQueryParameterUtils(tech.pegasys.teku.beaconrestapi.SingleQueryParameterUtils) Handler(io.javalin.http.Handler) List(java.util.List) OpenApiParam(io.javalin.plugin.openapi.annotations.OpenApiParam) RES_OK(tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_OK) Optional(java.util.Optional) SC_BAD_REQUEST(javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST) GetBlockHeadersResponse(tech.pegasys.teku.api.response.v1.beacon.GetBlockHeadersResponse) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi) NotNull(org.jetbrains.annotations.NotNull) DataProvider(tech.pegasys.teku.api.DataProvider) BadRequest(tech.pegasys.teku.beaconrestapi.schema.BadRequest) List(java.util.List) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) Bytes32(org.apache.tuweni.bytes.Bytes32) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi)

Example 2 with JsonProvider

use of tech.pegasys.teku.provider.JsonProvider in project teku by ConsenSys.

the class PostBlock method handle.

@OpenApi(path = ROUTE, method = HttpMethod.POST, summary = "Publish a signed block", tags = { TAG_BEACON, TAG_VALIDATOR_REQUIRED }, requestBody = @OpenApiRequestBody(content = { @OpenApiContent(from = SignedBlock.class) }), description = "Submit a signed beacon block to the beacon node to be imported." + " The beacon node performs the required validation.", responses = { @OpenApiResponse(status = RES_OK, description = "Block has been successfully broadcast, validated and imported."), @OpenApiResponse(status = RES_ACCEPTED, description = "Block has been successfully broadcast, but failed validation and has not been imported."), @OpenApiResponse(status = RES_BAD_REQUEST, description = "Unable to parse request body."), @OpenApiResponse(status = RES_INTERNAL_ERROR, description = "Beacon node experienced an internal error."), @OpenApiResponse(status = RES_SERVICE_UNAVAILABLE, description = "Beacon node is currently syncing.") })
@Override
public void handle(final Context ctx) throws Exception {
    try {
        if (syncDataProvider.isSyncing()) {
            ctx.status(SC_SERVICE_UNAVAILABLE);
            ctx.json(BadRequest.serviceUnavailable(jsonProvider));
            return;
        }
        final SignedBeaconBlock signedBeaconBlock = validatorDataProvider.parseBlock(jsonProvider, ctx.body());
        ctx.future(validatorDataProvider.submitSignedBlock(signedBeaconBlock).thenApplyChecked(validatorBlockResult -> handleResponseContext(ctx, validatorBlockResult)));
    } catch (final JsonProcessingException ex) {
        ctx.status(SC_BAD_REQUEST);
        ctx.json(BadRequest.badRequest(jsonProvider, ex.getMessage()));
    } catch (final Exception ex) {
        LOG.error("Failed to post block due to internal error", ex);
        ctx.status(SC_INTERNAL_SERVER_ERROR);
        ctx.json(BadRequest.internalError(jsonProvider, ex.getMessage()));
    }
}
Also used : SC_INTERNAL_SERVER_ERROR(javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR) RES_SERVICE_UNAVAILABLE(tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_SERVICE_UNAVAILABLE) TAG_BEACON(tech.pegasys.teku.infrastructure.http.RestApiConstants.TAG_BEACON) OpenApiContent(io.javalin.plugin.openapi.annotations.OpenApiContent) SC_SERVICE_UNAVAILABLE(javax.servlet.http.HttpServletResponse.SC_SERVICE_UNAVAILABLE) ValidatorDataProvider(tech.pegasys.teku.api.ValidatorDataProvider) RES_INTERNAL_ERROR(tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_INTERNAL_ERROR) RES_BAD_REQUEST(tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_BAD_REQUEST) Context(io.javalin.http.Context) OpenApiResponse(io.javalin.plugin.openapi.annotations.OpenApiResponse) JsonProvider(tech.pegasys.teku.provider.JsonProvider) RES_ACCEPTED(tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_ACCEPTED) SignedBlock(tech.pegasys.teku.api.schema.interfaces.SignedBlock) HttpMethod(io.javalin.plugin.openapi.annotations.HttpMethod) SyncDataProvider(tech.pegasys.teku.api.SyncDataProvider) BadRequest(tech.pegasys.teku.beaconrestapi.schema.BadRequest) TAG_VALIDATOR_REQUIRED(tech.pegasys.teku.infrastructure.http.RestApiConstants.TAG_VALIDATOR_REQUIRED) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Handler(io.javalin.http.Handler) Logger(org.apache.logging.log4j.Logger) SignedBeaconBlock(tech.pegasys.teku.api.schema.SignedBeaconBlock) SC_OK(javax.servlet.http.HttpServletResponse.SC_OK) RES_OK(tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_OK) SC_BAD_REQUEST(javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi) OpenApiRequestBody(io.javalin.plugin.openapi.annotations.OpenApiRequestBody) ValidatorBlockResult(tech.pegasys.teku.api.schema.ValidatorBlockResult) SC_ACCEPTED(javax.servlet.http.HttpServletResponse.SC_ACCEPTED) LogManager(org.apache.logging.log4j.LogManager) DataProvider(tech.pegasys.teku.api.DataProvider) SignedBeaconBlock(tech.pegasys.teku.api.schema.SignedBeaconBlock) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi)

Example 3 with JsonProvider

use of tech.pegasys.teku.provider.JsonProvider in project teku by ConsenSys.

the class ValidatorClientService method initializeValidators.

private void initializeValidators(ValidatorClientConfiguration config, ValidatorApiChannel validatorApiChannel, AsyncRunner asyncRunner) {
    validatorLoader.loadValidators();
    final OwnedValidators validators = validatorLoader.getOwnedValidators();
    this.validatorIndexProvider = new ValidatorIndexProvider(validators, validatorApiChannel, asyncRunner);
    final BlockDutyFactory blockDutyFactory = new BlockDutyFactory(forkProvider, validatorApiChannel, spec);
    final AttestationDutyFactory attestationDutyFactory = new AttestationDutyFactory(spec, forkProvider, validatorApiChannel);
    final BeaconCommitteeSubscriptions beaconCommitteeSubscriptions = new BeaconCommitteeSubscriptions(validatorApiChannel);
    final DutyLoader<?> attestationDutyLoader = new RetryingDutyLoader<>(asyncRunner, new AttestationDutyLoader(validatorApiChannel, forkProvider, dependentRoot -> new SlotBasedScheduledDuties<>(attestationDutyFactory, dependentRoot), validators, validatorIndexProvider, beaconCommitteeSubscriptions, spec));
    final DutyLoader<?> blockDutyLoader = new RetryingDutyLoader<>(asyncRunner, new BlockProductionDutyLoader(validatorApiChannel, dependentRoot -> new SlotBasedScheduledDuties<>(blockDutyFactory, dependentRoot), validators, validatorIndexProvider));
    validatorTimingChannels.add(new BlockDutyScheduler(metricsSystem, blockDutyLoader, spec));
    validatorTimingChannels.add(new AttestationDutyScheduler(metricsSystem, attestationDutyLoader, spec));
    validatorTimingChannels.add(validatorLoader.getSlashingProtectionLogger());
    if (spec.isMilestoneSupported(SpecMilestone.ALTAIR)) {
        final ChainHeadTracker chainHeadTracker = new ChainHeadTracker();
        validatorTimingChannels.add(chainHeadTracker);
        final DutyLoader<SyncCommitteeScheduledDuties> syncCommitteeDutyLoader = new RetryingDutyLoader<>(asyncRunner, new SyncCommitteeDutyLoader(validators, validatorIndexProvider, spec, validatorApiChannel, chainHeadTracker, forkProvider));
        validatorTimingChannels.add(new SyncCommitteeScheduler(metricsSystem, spec, syncCommitteeDutyLoader, new Random()::nextInt));
    }
    if (spec.isMilestoneSupported(SpecMilestone.BELLATRIX)) {
        proposerConfigProvider = Optional.of(ProposerConfigProvider.create(asyncRunner, config.getValidatorConfig().getRefreshProposerConfigFromSource(), new ProposerConfigLoader(new JsonProvider().getObjectMapper()), config.getValidatorConfig().getProposerConfigSource()));
        validatorTimingChannels.add(new BeaconProposerPreparer(validatorApiChannel, validatorIndexProvider, proposerConfigProvider.get(), config.getValidatorConfig().getProposerDefaultFeeRecipient(), spec));
    } else {
        proposerConfigProvider = Optional.empty();
    }
    addValidatorCountMetric(metricsSystem, validators);
    this.validatorStatusLogger = new DefaultValidatorStatusLogger(metricsSystem, validators, validatorApiChannel, asyncRunner);
}
Also used : AttestationDutyFactory(tech.pegasys.teku.validator.client.duties.attestations.AttestationDutyFactory) BlockDutyFactory(tech.pegasys.teku.validator.client.duties.BlockDutyFactory) ValidatorLoader(tech.pegasys.teku.validator.client.loader.ValidatorLoader) DataDirLayout(tech.pegasys.teku.service.serviceutils.layout.DataDirLayout) SafeFuture(tech.pegasys.teku.infrastructure.async.SafeFuture) ValidatorRestApiConfig(tech.pegasys.teku.validator.client.restapi.ValidatorRestApiConfig) Random(java.util.Random) LocalSlashingProtector(tech.pegasys.teku.core.signatures.LocalSlashingProtector) ArrayList(java.util.ArrayList) EventChannels(tech.pegasys.teku.infrastructure.events.EventChannels) ValidatorRestApi(tech.pegasys.teku.validator.client.restapi.ValidatorRestApi) PublicKeyLoader(tech.pegasys.teku.validator.client.loader.PublicKeyLoader) SlotBasedScheduledDuties(tech.pegasys.teku.validator.client.duties.SlotBasedScheduledDuties) JsonProvider(tech.pegasys.teku.provider.JsonProvider) ChainHeadTracker(tech.pegasys.teku.validator.client.duties.synccommittee.ChainHeadTracker) BeaconCommitteeSubscriptions(tech.pegasys.teku.validator.client.duties.BeaconCommitteeSubscriptions) Spec(tech.pegasys.teku.spec.Spec) Path(java.nio.file.Path) Service(tech.pegasys.teku.service.serviceutils.Service) ProposerConfigProvider(tech.pegasys.teku.validator.client.proposerconfig.ProposerConfigProvider) AsyncRunner(tech.pegasys.teku.infrastructure.async.AsyncRunner) ValidatorLogger(tech.pegasys.teku.infrastructure.logging.ValidatorLogger) SyncDataAccessor(tech.pegasys.teku.infrastructure.io.SyncDataAccessor) RestApi(tech.pegasys.teku.infrastructure.restapi.RestApi) ServiceConfig(tech.pegasys.teku.service.serviceutils.ServiceConfig) BeaconNodeApi(tech.pegasys.teku.validator.beaconnode.BeaconNodeApi) SyncCommitteeScheduledDuties(tech.pegasys.teku.validator.client.duties.synccommittee.SyncCommitteeScheduledDuties) List(java.util.List) Logger(org.apache.logging.log4j.Logger) TekuMetricCategory(tech.pegasys.teku.infrastructure.metrics.TekuMetricCategory) ValidatorApiChannel(tech.pegasys.teku.validator.api.ValidatorApiChannel) InProcessBeaconNodeApi(tech.pegasys.teku.validator.eventadapter.InProcessBeaconNodeApi) OwnedValidators(tech.pegasys.teku.validator.client.loader.OwnedValidators) SlashingProtector(tech.pegasys.teku.core.signatures.SlashingProtector) SystemSignalListener(tech.pegasys.teku.infrastructure.io.SystemSignalListener) GenesisDataProvider(tech.pegasys.teku.validator.beaconnode.GenesisDataProvider) ProposerConfigLoader(tech.pegasys.teku.validator.client.proposerconfig.loader.ProposerConfigLoader) SlashingProtectionLogger(tech.pegasys.teku.validator.client.loader.SlashingProtectionLogger) Optional(java.util.Optional) RemoteBeaconNodeApi(tech.pegasys.teku.validator.remote.RemoteBeaconNodeApi) MetricsSystem(org.hyperledger.besu.plugin.services.MetricsSystem) LogManager(org.apache.logging.log4j.LogManager) SpecMilestone(tech.pegasys.teku.spec.SpecMilestone) ValidatorTimingChannel(tech.pegasys.teku.validator.api.ValidatorTimingChannel) AttestationDutyFactory(tech.pegasys.teku.validator.client.duties.attestations.AttestationDutyFactory) BlockDutyFactory(tech.pegasys.teku.validator.client.duties.BlockDutyFactory) ChainHeadTracker(tech.pegasys.teku.validator.client.duties.synccommittee.ChainHeadTracker) OwnedValidators(tech.pegasys.teku.validator.client.loader.OwnedValidators) BeaconCommitteeSubscriptions(tech.pegasys.teku.validator.client.duties.BeaconCommitteeSubscriptions) SyncCommitteeScheduledDuties(tech.pegasys.teku.validator.client.duties.synccommittee.SyncCommitteeScheduledDuties) ProposerConfigLoader(tech.pegasys.teku.validator.client.proposerconfig.loader.ProposerConfigLoader) JsonProvider(tech.pegasys.teku.provider.JsonProvider) SlotBasedScheduledDuties(tech.pegasys.teku.validator.client.duties.SlotBasedScheduledDuties) Random(java.util.Random)

Example 4 with JsonProvider

use of tech.pegasys.teku.provider.JsonProvider in project teku by ConsenSys.

the class PostVoluntaryExit method handle.

@OpenApi(path = ROUTE, method = HttpMethod.POST, summary = "Submit signed voluntary exit", tags = { TAG_BEACON }, description = "Submits signed voluntary exit object to node's pool and if it passes validation node MUST broadcast it to network.", requestBody = @OpenApiRequestBody(content = { @OpenApiContent(from = SignedVoluntaryExit.class) }), responses = { @OpenApiResponse(status = RES_OK, description = "Signed voluntary exit has been successfully validated, added to the pool, and broadcast."), @OpenApiResponse(status = RES_BAD_REQUEST, description = "Invalid voluntary exit, it will never pass validation so it's rejected"), @OpenApiResponse(status = RES_INTERNAL_ERROR) })
@Override
public void handle(final Context ctx) throws Exception {
    try {
        final SignedVoluntaryExit exit = parseRequestBody(ctx.body(), SignedVoluntaryExit.class);
        ctx.future(nodeDataProvider.postVoluntaryExit(exit).thenApplyChecked(result -> handleResponseContext(ctx, result)));
    } catch (final IllegalArgumentException e) {
        LOG.debug("Voluntary exit failed", e);
        ctx.json(BadRequest.badRequest(jsonProvider, e.getMessage()));
        ctx.status(SC_BAD_REQUEST);
    }
}
Also used : SC_BAD_REQUEST(tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_BAD_REQUEST) HttpMethod(io.javalin.plugin.openapi.annotations.HttpMethod) SignedVoluntaryExit(tech.pegasys.teku.api.schema.SignedVoluntaryExit) AbstractHandler(tech.pegasys.teku.beaconrestapi.handlers.AbstractHandler) BadRequest(tech.pegasys.teku.beaconrestapi.schema.BadRequest) TAG_BEACON(tech.pegasys.teku.infrastructure.http.RestApiConstants.TAG_BEACON) OpenApiContent(io.javalin.plugin.openapi.annotations.OpenApiContent) ValidationResultCode(tech.pegasys.teku.statetransition.validation.ValidationResultCode) RES_INTERNAL_ERROR(tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_INTERNAL_ERROR) Logger(org.apache.logging.log4j.Logger) RES_BAD_REQUEST(tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_BAD_REQUEST) Context(io.javalin.http.Context) OpenApiResponse(io.javalin.plugin.openapi.annotations.OpenApiResponse) RES_OK(tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_OK) JsonProvider(tech.pegasys.teku.provider.JsonProvider) NodeDataProvider(tech.pegasys.teku.api.NodeDataProvider) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi) OpenApiRequestBody(io.javalin.plugin.openapi.annotations.OpenApiRequestBody) LogManager(org.apache.logging.log4j.LogManager) DataProvider(tech.pegasys.teku.api.DataProvider) InternalValidationResult(tech.pegasys.teku.statetransition.validation.InternalValidationResult) SC_OK(tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_OK) SignedVoluntaryExit(tech.pegasys.teku.api.schema.SignedVoluntaryExit) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi)

Aggregations

JsonProvider (tech.pegasys.teku.provider.JsonProvider)4 Context (io.javalin.http.Context)3 HttpMethod (io.javalin.plugin.openapi.annotations.HttpMethod)3 OpenApi (io.javalin.plugin.openapi.annotations.OpenApi)3 OpenApiContent (io.javalin.plugin.openapi.annotations.OpenApiContent)3 OpenApiResponse (io.javalin.plugin.openapi.annotations.OpenApiResponse)3 LogManager (org.apache.logging.log4j.LogManager)3 Logger (org.apache.logging.log4j.Logger)3 DataProvider (tech.pegasys.teku.api.DataProvider)3 BadRequest (tech.pegasys.teku.beaconrestapi.schema.BadRequest)3 RES_BAD_REQUEST (tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_BAD_REQUEST)3 RES_INTERNAL_ERROR (tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_INTERNAL_ERROR)3 RES_OK (tech.pegasys.teku.infrastructure.http.RestApiConstants.RES_OK)3 TAG_BEACON (tech.pegasys.teku.infrastructure.http.RestApiConstants.TAG_BEACON)3 Handler (io.javalin.http.Handler)2 OpenApiRequestBody (io.javalin.plugin.openapi.annotations.OpenApiRequestBody)2 List (java.util.List)2 Optional (java.util.Optional)2 SC_BAD_REQUEST (javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST)2 SafeFuture (tech.pegasys.teku.infrastructure.async.SafeFuture)2