Search in sources :

Example 1 with JsonRpcErrorResponse

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

the class QbftDiscardValidatorVoteTest method methodNotEnabledWhenNoVoteProvider.

@Test
public void methodNotEnabledWhenNoVoteProvider() {
    final JsonRpcRequestContext request = requestWithParams(Address.fromHexString("1"));
    final JsonRpcResponse expectedResponse = new JsonRpcErrorResponse(request.getRequest().getId(), JsonRpcError.METHOD_NOT_ENABLED);
    when(validatorProvider.getVoteProviderAtHead()).thenReturn(Optional.empty());
    final JsonRpcResponse response = method.response(request);
    assertThat(response).usingRecursiveComparison().isEqualTo(expectedResponse);
}
Also used : JsonRpcResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse) JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) JsonRpcErrorResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse) Test(org.junit.Test)

Example 2 with JsonRpcErrorResponse

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

the class WebSocketMessageHandler method handle.

public void handle(final ServerWebSocket websocket, final Buffer buffer, final Optional<User> user) {
    if (buffer.length() == 0) {
        replyToClient(websocket, errorResponse(null, JsonRpcError.INVALID_REQUEST));
    } else {
        try {
            final JsonObject jsonRpcRequest = buffer.toJsonObject();
            vertx.<JsonRpcResponse>executeBlocking(promise -> {
                try {
                    final JsonRpcResponse jsonRpcResponse = jsonRpcExecutor.execute(user, null, null, new IsAliveHandler(ethScheduler, timeoutSec), jsonRpcRequest, req -> {
                        final WebSocketRpcRequest websocketRequest = req.mapTo(WebSocketRpcRequest.class);
                        websocketRequest.setConnectionId(websocket.textHandlerID());
                        return websocketRequest;
                    });
                    promise.complete(jsonRpcResponse);
                } catch (RuntimeException e) {
                    promise.fail(e);
                }
            }).onSuccess(jsonRpcResponse -> replyToClient(websocket, jsonRpcResponse)).onFailure(throwable -> {
                try {
                    final Integer id = jsonRpcRequest.getInteger("id", null);
                    replyToClient(websocket, errorResponse(id, JsonRpcError.INTERNAL_ERROR));
                } catch (ClassCastException idNotIntegerException) {
                    replyToClient(websocket, errorResponse(null, JsonRpcError.INTERNAL_ERROR));
                }
            });
        } catch (DecodeException jsonObjectDecodeException) {
            try {
                final JsonArray batchJsonRpcRequest = buffer.toJsonArray();
                vertx.<List<JsonRpcResponse>>executeBlocking(promise -> {
                    List<JsonRpcResponse> responses = new ArrayList<>();
                    for (int i = 0; i < batchJsonRpcRequest.size(); i++) {
                        final JsonObject jsonRequest;
                        try {
                            jsonRequest = batchJsonRpcRequest.getJsonObject(i);
                        } catch (ClassCastException e) {
                            responses.add(new JsonRpcErrorResponse(null, INVALID_REQUEST));
                            continue;
                        }
                        responses.add(jsonRpcExecutor.execute(user, null, null, new IsAliveHandler(ethScheduler, timeoutSec), jsonRequest, req -> {
                            final WebSocketRpcRequest websocketRequest = req.mapTo(WebSocketRpcRequest.class);
                            websocketRequest.setConnectionId(websocket.textHandlerID());
                            return websocketRequest;
                        }));
                    }
                    promise.complete(responses);
                }).onSuccess(jsonRpcBatchResponse -> {
                    final JsonRpcResponse[] completed = jsonRpcBatchResponse.stream().filter(jsonRpcResponse -> jsonRpcResponse.getType() != JsonRpcResponseType.NONE).toArray(JsonRpcResponse[]::new);
                    replyToClient(websocket, completed);
                }).onFailure(throwable -> replyToClient(websocket, errorResponse(null, JsonRpcError.INTERNAL_ERROR)));
            } catch (RuntimeException jsonArrayDecodeException) {
                replyToClient(websocket, errorResponse(null, JsonRpcError.INTERNAL_ERROR));
            }
        }
    }
}
Also used : JsonRpcResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse) ServerWebSocket(io.vertx.core.http.ServerWebSocket) JsonRpcError(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError) DecodeException(io.vertx.core.json.DecodeException) LoggerFactory(org.slf4j.LoggerFactory) IsAliveHandler(org.hyperledger.besu.ethereum.api.handlers.IsAliveHandler) JsonRpcResponseType(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponseType) ArrayList(java.util.ArrayList) EthScheduler(org.hyperledger.besu.ethereum.eth.manager.EthScheduler) JsonObject(io.vertx.core.json.JsonObject) WebSocketRpcRequest(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.methods.WebSocketRpcRequest) Logger(org.slf4j.Logger) INVALID_REQUEST(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError.INVALID_REQUEST) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) JsonRpcResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Vertx(io.vertx.core.Vertx) IOException(java.io.IOException) JsonRpcErrorResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse) Jdk8Module(com.fasterxml.jackson.datatype.jdk8.Jdk8Module) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) Feature(com.fasterxml.jackson.core.JsonGenerator.Feature) User(io.vertx.ext.auth.User) Buffer(io.vertx.core.buffer.Buffer) Optional(java.util.Optional) JsonRpcExecutor(org.hyperledger.besu.ethereum.api.jsonrpc.execution.JsonRpcExecutor) JsonObject(io.vertx.core.json.JsonObject) WebSocketRpcRequest(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.methods.WebSocketRpcRequest) DecodeException(io.vertx.core.json.DecodeException) JsonArray(io.vertx.core.json.JsonArray) IsAliveHandler(org.hyperledger.besu.ethereum.api.handlers.IsAliveHandler) ArrayList(java.util.ArrayList) List(java.util.List) JsonRpcErrorResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse)

Example 3 with JsonRpcErrorResponse

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

the class EthUnsubscribe method response.

@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
    try {
        final UnsubscribeRequest unsubscribeRequest = getMapper().mapUnsubscribeRequest(requestContext);
        final boolean unsubscribed = subscriptionManager().unsubscribe(unsubscribeRequest);
        return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), unsubscribed);
    } catch (final InvalidSubscriptionRequestException isEx) {
        return new JsonRpcErrorResponse(requestContext.getRequest().getId(), JsonRpcError.INVALID_REQUEST);
    } catch (final SubscriptionNotFoundException snfEx) {
        return new JsonRpcErrorResponse(requestContext.getRequest().getId(), JsonRpcError.SUBSCRIPTION_NOT_FOUND);
    } catch (final Exception e) {
        return new JsonRpcErrorResponse(requestContext.getRequest().getId(), JsonRpcError.INTERNAL_ERROR);
    }
}
Also used : UnsubscribeRequest(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.request.UnsubscribeRequest) SubscriptionNotFoundException(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.SubscriptionNotFoundException) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) InvalidSubscriptionRequestException(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.request.InvalidSubscriptionRequestException) SubscriptionNotFoundException(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.SubscriptionNotFoundException) InvalidSubscriptionRequestException(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.request.InvalidSubscriptionRequestException) JsonRpcErrorResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse)

Example 4 with JsonRpcErrorResponse

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

the class PrivUnsubscribe method response.

@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
    try {
        final PrivateUnsubscribeRequest unsubscribeRequest = getMapper().mapPrivateUnsubscribeRequest(requestContext);
        if (privacyController instanceof MultiTenancyPrivacyController) {
            checkIfPrivacyGroupMatchesAuthenticatedPrivacyUserId(requestContext, unsubscribeRequest.getPrivacyGroupId());
        }
        final boolean unsubscribed = subscriptionManager().unsubscribe(unsubscribeRequest);
        return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), unsubscribed);
    } catch (final InvalidSubscriptionRequestException isEx) {
        return new JsonRpcErrorResponse(requestContext.getRequest().getId(), JsonRpcError.INVALID_REQUEST);
    } catch (final SubscriptionNotFoundException snfEx) {
        return new JsonRpcErrorResponse(requestContext.getRequest().getId(), JsonRpcError.SUBSCRIPTION_NOT_FOUND);
    }
}
Also used : PrivateUnsubscribeRequest(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.request.PrivateUnsubscribeRequest) SubscriptionNotFoundException(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.SubscriptionNotFoundException) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) InvalidSubscriptionRequestException(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.request.InvalidSubscriptionRequestException) MultiTenancyPrivacyController(org.hyperledger.besu.ethereum.privacy.MultiTenancyPrivacyController) JsonRpcErrorResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse)

Example 5 with JsonRpcErrorResponse

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

the class ProposeTest method testDropWithAddressZeroResultsInError.

@Test
public void testDropWithAddressZeroResultsInError() {
    final Propose propose = new Propose(validatorProvider);
    final Address a0 = Address.fromHexString("0");
    final JsonRpcResponse response = propose.response(requestWithParams(a0, false));
    assertThat(validatorProvider.getVoteProviderAtHead().get().getProposals().get(a0)).isNull();
    assertThat(response.getType()).isEqualTo(JsonRpcResponseType.ERROR);
    final JsonRpcErrorResponse errorResponse = (JsonRpcErrorResponse) response;
    assertThat(errorResponse.getError()).isEqualTo(JsonRpcError.INVALID_REQUEST);
}
Also used : JsonRpcResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse) Address(org.hyperledger.besu.datatypes.Address) JsonRpcErrorResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse) Test(org.junit.Test)

Aggregations

JsonRpcErrorResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse)251 JsonRpcResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse)166 Test (org.junit.Test)153 JsonRpcRequestContext (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext)146 JsonRpcSuccessResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse)56 JsonRpcRequest (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest)47 ArrayList (java.util.ArrayList)25 Test (org.junit.jupiter.api.Test)25 JsonObject (io.vertx.core.json.JsonObject)20 JsonRpcError (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError)19 NodesAllowlistResult (org.hyperledger.besu.ethereum.permissioning.NodeLocalConfigPermissioningController.NodesAllowlistResult)16 Optional (java.util.Optional)14 RpcMethod (org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod)14 MultiTenancyValidationException (org.hyperledger.besu.ethereum.privacy.MultiTenancyValidationException)14 Hash (org.hyperledger.besu.datatypes.Hash)13 JsonCallParameter (org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.JsonCallParameter)13 Async (io.vertx.ext.unit.Async)12 List (java.util.List)11 Address (org.hyperledger.besu.datatypes.Address)11 Logger (org.slf4j.Logger)10