Search in sources :

Example 1 with InvalidJsonRpcParameters

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

the class PluginsReloadConfiguration method response.

@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
    try {
        final String pluginName = requestContext.getRequiredParameter(0, String.class);
        if (!namedPlugins.containsKey(pluginName)) {
            LOG.error("Plugin cannot be reloaded because no plugin has been registered with specified name: {}.", pluginName);
            return new JsonRpcErrorResponse(requestContext.getRequest().getId(), JsonRpcError.PLUGIN_NOT_FOUND);
        }
        reloadPluginConfig(namedPlugins.get(pluginName));
        return new JsonRpcSuccessResponse(requestContext.getRequest().getId());
    } catch (InvalidJsonRpcParameters invalidJsonRpcParameters) {
        return new JsonRpcErrorResponse(requestContext.getRequest().getId(), JsonRpcError.INVALID_PARAMS);
    }
}
Also used : InvalidJsonRpcParameters(org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcParameters) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) JsonRpcErrorResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse)

Example 2 with InvalidJsonRpcParameters

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

the class JsonRpcParameter method optional.

/**
 * Retrieves an optional parameter at the given index interpreted as the given class. Throws
 * InvalidJsonRpcParameters if parameter is of the wrong type.
 *
 * @param params the list of objects from which to extract a typed object.
 * @param index Which index of the params array to access.
 * @param paramClass What type is expected at this index.
 * @param <T> The type of parameter.
 * @return Returns the parameter cast as T if available.
 */
@SuppressWarnings("unchecked")
public <T> Optional<T> optional(final Object[] params, final int index, final Class<T> paramClass) {
    if (params == null || params.length <= index || params[index] == null) {
        return Optional.empty();
    }
    final T param;
    final Object rawParam = params[index];
    if (paramClass.isAssignableFrom(rawParam.getClass())) {
        // If we're dealing with a simple type, just cast the value
        param = (T) rawParam;
    } else {
        // Otherwise, serialize param back to json and then deserialize to the paramClass type
        try {
            final String json = mapper.writeValueAsString(rawParam);
            param = mapper.readValue(json, paramClass);
        } catch (final JsonProcessingException e) {
            throw new InvalidJsonRpcParameters("Invalid json rpc parameter at index " + index, e);
        }
    }
    return Optional.of(param);
}
Also used : InvalidJsonRpcParameters(org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcParameters) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 3 with InvalidJsonRpcParameters

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

the class WebSocketMessageHandlerTest method onInvalidJsonRpcParametersExceptionProcessingRequestShouldRespondInvalidParams.

@Test
public void onInvalidJsonRpcParametersExceptionProcessingRequestShouldRespondInvalidParams(final TestContext context) {
    final Async async = context.async();
    final JsonObject requestJson = new JsonObject().put("id", 1).put("method", "eth_x");
    final JsonRpcRequestContext expectedRequest = new JsonRpcRequestContext(requestJson.mapTo(WebSocketRpcRequest.class));
    when(jsonRpcMethodMock.response(eq(expectedRequest))).thenThrow(new InvalidJsonRpcParameters(""));
    final JsonRpcErrorResponse expectedResponse = new JsonRpcErrorResponse(1, JsonRpcError.INVALID_PARAMS);
    when(websocketMock.writeFrame(argThat(this::isFinalFrame))).then(completeOnLastFrame(async));
    handler.handle(websocketMock, requestJson.toBuffer(), Optional.empty());
    async.awaitSuccess(WebSocketMessageHandlerTest.VERTX_AWAIT_TIMEOUT_MILLIS);
    // can verify only after async not before
    verify(websocketMock).writeFrame(argThat(isFrameWithText(Json.encode(expectedResponse))));
    verify(websocketMock).writeFrame(argThat(this::isFinalFrame));
}
Also used : JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) Async(io.vertx.ext.unit.Async) JsonObject(io.vertx.core.json.JsonObject) InvalidJsonRpcParameters(org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcParameters) WebSocketRpcRequest(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.methods.WebSocketRpcRequest) JsonRpcErrorResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse) Test(org.junit.Test)

Example 4 with InvalidJsonRpcParameters

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

the class AdminChangeLogLevel method response.

@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
    try {
        final String rawLogLevel = requestContext.getRequiredParameter(0, String.class);
        if (!VALID_PARAMS.contains(rawLogLevel)) {
            return new JsonRpcErrorResponse(requestContext.getRequest().getId(), JsonRpcError.INVALID_PARAMS);
        }
        final Level logLevel = Level.toLevel(rawLogLevel);
        final Optional<String[]> optionalLogFilters = requestContext.getOptionalParameter(1, String[].class);
        optionalLogFilters.ifPresentOrElse(logFilters -> Arrays.stream(logFilters).forEach(logFilter -> setLogLevel(logFilter, logLevel)), () -> setLogLevel("", logLevel));
        return new JsonRpcSuccessResponse(requestContext.getRequest().getId());
    } catch (InvalidJsonRpcParameters invalidJsonRpcParameters) {
        return new JsonRpcErrorResponse(requestContext.getRequest().getId(), JsonRpcError.INVALID_PARAMS);
    }
}
Also used : Log4j2ConfiguratorUtil(org.hyperledger.besu.util.Log4j2ConfiguratorUtil) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) JsonRpcError(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError) JsonRpcResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse) LoggerFactory(org.slf4j.LoggerFactory) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) Set(java.util.Set) Level(org.apache.logging.log4j.Level) JsonRpcErrorResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse) InvalidJsonRpcParameters(org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcParameters) RpcMethod(org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod) JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) Optional(java.util.Optional) Level(org.apache.logging.log4j.Level) InvalidJsonRpcParameters(org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcParameters) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) JsonRpcErrorResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse)

Example 5 with InvalidJsonRpcParameters

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

the class WebSocketRequestHandlerTest method onInvalidJsonRpcParametersExceptionProcessingRequestShouldRespondInvalidParams.

@Test
public void onInvalidJsonRpcParametersExceptionProcessingRequestShouldRespondInvalidParams(final TestContext context) {
    final Async async = context.async();
    final JsonObject requestJson = new JsonObject().put("id", 1).put("method", "eth_x");
    final JsonRpcRequestContext expectedRequest = new JsonRpcRequestContext(requestJson.mapTo(WebSocketRpcRequest.class));
    when(jsonRpcMethodMock.response(eq(expectedRequest))).thenThrow(new InvalidJsonRpcParameters(""));
    final JsonRpcErrorResponse expectedResponse = new JsonRpcErrorResponse(1, JsonRpcError.INVALID_PARAMS);
    when(websocketMock.writeFrame(argThat(this::isFinalFrame))).then(completeOnLastFrame(async));
    handler.handle(websocketMock, requestJson.toString());
    async.awaitSuccess(WebSocketRequestHandlerTest.VERTX_AWAIT_TIMEOUT_MILLIS);
    // can verify only after async not before
    verify(websocketMock).writeFrame(argThat(isFrameWithText(Json.encode(expectedResponse))));
    verify(websocketMock).writeFrame(argThat(this::isFinalFrame));
}
Also used : JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) Async(io.vertx.ext.unit.Async) JsonObject(io.vertx.core.json.JsonObject) InvalidJsonRpcParameters(org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcParameters) WebSocketRpcRequest(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.methods.WebSocketRpcRequest) JsonRpcErrorResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse) Test(org.junit.Test)

Aggregations

InvalidJsonRpcParameters (org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcParameters)7 JsonRpcRequestContext (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext)5 JsonRpcErrorResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse)5 JsonObject (io.vertx.core.json.JsonObject)3 Async (io.vertx.ext.unit.Async)2 JsonRpcMethod (org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod)2 JsonRpcError (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError)2 JsonRpcSuccessResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse)2 WebSocketRpcRequest (org.hyperledger.besu.ethereum.api.jsonrpc.websocket.methods.WebSocketRpcRequest)2 Test (org.junit.Test)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Span (io.opentelemetry.api.trace.Span)1 DecodeException (io.vertx.core.json.DecodeException)1 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Level (org.apache.logging.log4j.Level)1 IsAliveHandler (org.hyperledger.besu.ethereum.api.handlers.IsAliveHandler)1 RpcMethodTimeoutException (org.hyperledger.besu.ethereum.api.handlers.RpcMethodTimeoutException)1