Search in sources :

Example 1 with JsonRpcRequestId

use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestId in project besu by hyperledger.

the class JsonRpcExecutor method execute.

public JsonRpcResponse execute(final Optional<User> optionalUser, final Tracer tracer, final Context spanContext, final Supplier<Boolean> alive, final JsonObject jsonRpcRequest, final Function<JsonObject, JsonRpcRequest> requestBodyProvider) {
    try {
        final JsonRpcRequest requestBody = requestBodyProvider.apply(jsonRpcRequest);
        final JsonRpcRequestId id = new JsonRpcRequestId(requestBody.getId());
        // Handle notifications
        if (requestBody.isNotification()) {
            // Notifications aren't handled so create empty result for now.
            return new JsonRpcNoResponse();
        }
        final Span span;
        if (tracer != null) {
            span = tracer.spanBuilder(requestBody.getMethod()).setSpanKind(SpanKind.INTERNAL).setParent(spanContext).startSpan();
        } else {
            span = Span.getInvalid();
        }
        final Optional<JsonRpcError> unavailableMethod = validateMethodAvailability(requestBody);
        if (unavailableMethod.isPresent()) {
            span.setStatus(StatusCode.ERROR, "method unavailable");
            return new JsonRpcErrorResponse(id, unavailableMethod.get());
        }
        final JsonRpcMethod method = rpcMethods.get(requestBody.getMethod());
        return rpcProcessor.process(id, method, span, new JsonRpcRequestContext(requestBody, optionalUser, alive));
    } catch (IllegalArgumentException e) {
        try {
            final Integer id = jsonRpcRequest.getInteger("id", null);
            return new JsonRpcErrorResponse(id, INVALID_REQUEST);
        } catch (ClassCastException idNotIntegerException) {
            return new JsonRpcErrorResponse(null, INVALID_REQUEST);
        }
    }
}
Also used : JsonRpcRequestId(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestId) JsonRpcRequest(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest) JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) JsonRpcMethod(org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod) JsonRpcError(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError) Span(io.opentelemetry.api.trace.Span) JsonRpcNoResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcNoResponse) JsonRpcErrorResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse)

Example 2 with JsonRpcRequestId

use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestId in project besu by hyperledger.

the class JsonRpcHttpService method process.

private JsonRpcResponse process(final RoutingContext ctx, final JsonObject requestJson, final Optional<User> user) {
    final JsonRpcRequest requestBody;
    Object id = null;
    try {
        id = new JsonRpcRequestId(requestJson.getValue("id")).getValue();
        requestBody = requestJson.mapTo(JsonRpcRequest.class);
    } catch (final IllegalArgumentException exception) {
        return errorResponse(id, INVALID_REQUEST);
    }
    Span span = tracer.spanBuilder(requestBody.getMethod()).setSpanKind(SpanKind.INTERNAL).setParent(ctx.get(SPAN_CONTEXT)).startSpan();
    try {
        // Handle notifications
        if (requestBody.isNotification()) {
            // Notifications aren't handled so create empty result for now.
            return NO_RESPONSE;
        }
        final Optional<JsonRpcError> unavailableMethod = validateMethodAvailability(requestBody);
        if (unavailableMethod.isPresent()) {
            span.setStatus(StatusCode.ERROR, "method unavailable");
            return errorResponse(id, unavailableMethod.get());
        }
        final JsonRpcMethod method = rpcMethods.get(requestBody.getMethod());
        if (!authenticationService.isPresent() || (authenticationService.isPresent() && authenticationService.get().isPermitted(user, method, config.getNoAuthRpcApis()))) {
            // Generate response
            try (final OperationTimer.TimingContext ignored = requestTimer.labels(requestBody.getMethod()).startTimer()) {
                if (user.isPresent()) {
                    return method.response(new JsonRpcRequestContext(requestBody, user.get(), () -> !ctx.response().closed()));
                }
                return method.response(new JsonRpcRequestContext(requestBody, () -> !ctx.response().closed()));
            } catch (final InvalidJsonRpcParameters e) {
                LOG.debug("Invalid Params", e);
                span.setStatus(StatusCode.ERROR, "Invalid Params");
                return errorResponse(id, JsonRpcError.INVALID_PARAMS);
            } catch (final MultiTenancyValidationException e) {
                span.setStatus(StatusCode.ERROR, "Unauthorized");
                return unauthorizedResponse(id, JsonRpcError.UNAUTHORIZED);
            } catch (final RuntimeException e) {
                LOG.error("Error processing JSON-RPC requestBody", e);
                span.setStatus(StatusCode.ERROR, "Error processing JSON-RPC requestBody");
                return errorResponse(id, JsonRpcError.INTERNAL_ERROR);
            }
        } else {
            span.setStatus(StatusCode.ERROR, "Unauthorized");
            return unauthorizedResponse(id, JsonRpcError.UNAUTHORIZED);
        }
    } finally {
        span.end();
    }
}
Also used : OperationTimer(org.hyperledger.besu.plugin.services.metrics.OperationTimer) JsonRpcRequest(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest) JsonRpcError(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError) InvalidJsonRpcParameters(org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcParameters) Span(io.opentelemetry.api.trace.Span) MultiTenancyValidationException(org.hyperledger.besu.ethereum.privacy.MultiTenancyValidationException) JsonRpcRequestId(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestId) JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) JsonRpcMethod(org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod) JsonObject(io.vertx.core.json.JsonObject)

Aggregations

Span (io.opentelemetry.api.trace.Span)2 JsonRpcRequest (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest)2 JsonRpcRequestContext (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext)2 JsonRpcRequestId (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestId)2 JsonRpcMethod (org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod)2 JsonRpcError (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError)2 JsonObject (io.vertx.core.json.JsonObject)1 InvalidJsonRpcParameters (org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcParameters)1 JsonRpcErrorResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse)1 JsonRpcNoResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcNoResponse)1 MultiTenancyValidationException (org.hyperledger.besu.ethereum.privacy.MultiTenancyValidationException)1 OperationTimer (org.hyperledger.besu.plugin.services.metrics.OperationTimer)1