Search in sources :

Example 1 with WebSocketRpcRequest

use of org.hyperledger.besu.ethereum.api.jsonrpc.websocket.methods.WebSocketRpcRequest 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 2 with WebSocketRpcRequest

use of org.hyperledger.besu.ethereum.api.jsonrpc.websocket.methods.WebSocketRpcRequest in project besu by hyperledger.

the class SubscriptionRequestMapper method mapSubscribeRequest.

public SubscribeRequest mapSubscribeRequest(final JsonRpcRequestContext jsonRpcRequestContext) throws InvalidSubscriptionRequestException {
    try {
        final WebSocketRpcRequest webSocketRpcRequestBody = validateRequest(jsonRpcRequestContext);
        final SubscriptionType subscriptionType = webSocketRpcRequestBody.getRequiredParameter(0, SubscriptionType.class);
        switch(subscriptionType) {
            case NEW_BLOCK_HEADERS:
                {
                    final boolean includeTransactions = includeTransactions(webSocketRpcRequestBody);
                    return parseNewBlockHeadersRequest(webSocketRpcRequestBody, includeTransactions);
                }
            case LOGS:
                {
                    return parseLogsRequest(webSocketRpcRequestBody);
                }
            case NEW_PENDING_TRANSACTIONS:
            case SYNCING:
            default:
                final boolean includeTransactions = includeTransactions(webSocketRpcRequestBody);
                return new SubscribeRequest(subscriptionType, null, includeTransactions, webSocketRpcRequestBody.getConnectionId());
        }
    } catch (final Exception e) {
        throw new InvalidSubscriptionRequestException("Error parsing subscribe request", e);
    }
}
Also used : WebSocketRpcRequest(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.methods.WebSocketRpcRequest)

Example 3 with WebSocketRpcRequest

use of org.hyperledger.besu.ethereum.api.jsonrpc.websocket.methods.WebSocketRpcRequest in project besu by hyperledger.

the class SubscriptionRequestMapper method mapPrivateSubscribeRequest.

public PrivateSubscribeRequest mapPrivateSubscribeRequest(final JsonRpcRequestContext jsonRpcRequestContext, final String privacyUserId) throws InvalidSubscriptionRequestException {
    try {
        final WebSocketRpcRequest webSocketRpcRequestBody = validateRequest(jsonRpcRequestContext);
        final String privacyGroupId = webSocketRpcRequestBody.getRequiredParameter(0, String.class);
        final SubscriptionType subscriptionType = webSocketRpcRequestBody.getRequiredParameter(1, SubscriptionType.class);
        switch(subscriptionType) {
            case LOGS:
                {
                    final FilterParameter filterParameter = jsonRpcRequestContext.getRequiredParameter(2, FilterParameter.class);
                    return new PrivateSubscribeRequest(SubscriptionType.LOGS, filterParameter, null, webSocketRpcRequestBody.getConnectionId(), privacyGroupId, privacyUserId);
                }
            default:
                throw new InvalidSubscriptionRequestException("Invalid subscribe request. Invalid private subscription type.");
        }
    } catch (final InvalidSubscriptionRequestException e) {
        throw e;
    } catch (final Exception e) {
        throw new InvalidSubscriptionRequestException("Error parsing subscribe request", e);
    }
}
Also used : WebSocketRpcRequest(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.methods.WebSocketRpcRequest) FilterParameter(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.FilterParameter)

Example 4 with WebSocketRpcRequest

use of org.hyperledger.besu.ethereum.api.jsonrpc.websocket.methods.WebSocketRpcRequest in project besu by hyperledger.

the class SubscriptionRequestMapper method mapUnsubscribeRequest.

public UnsubscribeRequest mapUnsubscribeRequest(final JsonRpcRequestContext jsonRpcRequestContext) throws InvalidSubscriptionRequestException {
    try {
        final WebSocketRpcRequest webSocketRpcRequestBody = validateRequest(jsonRpcRequestContext);
        final long subscriptionId = webSocketRpcRequestBody.getRequiredParameter(0, UnsignedLongParameter.class).getValue();
        return new UnsubscribeRequest(subscriptionId, webSocketRpcRequestBody.getConnectionId());
    } catch (final Exception e) {
        throw new InvalidSubscriptionRequestException("Error parsing unsubscribe request", e);
    }
}
Also used : UnsignedLongParameter(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.UnsignedLongParameter) WebSocketRpcRequest(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.methods.WebSocketRpcRequest)

Example 5 with WebSocketRpcRequest

use of org.hyperledger.besu.ethereum.api.jsonrpc.websocket.methods.WebSocketRpcRequest in project besu by hyperledger.

the class SubscriptionRequestMapper method mapPrivateUnsubscribeRequest.

public PrivateUnsubscribeRequest mapPrivateUnsubscribeRequest(final JsonRpcRequestContext jsonRpcRequestContext) throws InvalidSubscriptionRequestException {
    try {
        final WebSocketRpcRequest webSocketRpcRequestBody = validateRequest(jsonRpcRequestContext);
        final String privacyGroupId = webSocketRpcRequestBody.getRequiredParameter(0, String.class);
        final long subscriptionId = webSocketRpcRequestBody.getRequiredParameter(1, UnsignedLongParameter.class).getValue();
        return new PrivateUnsubscribeRequest(subscriptionId, webSocketRpcRequestBody.getConnectionId(), privacyGroupId);
    } catch (final Exception e) {
        throw new InvalidSubscriptionRequestException("Error parsing unsubscribe request", e);
    }
}
Also used : UnsignedLongParameter(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.UnsignedLongParameter) WebSocketRpcRequest(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.methods.WebSocketRpcRequest)

Aggregations

WebSocketRpcRequest (org.hyperledger.besu.ethereum.api.jsonrpc.websocket.methods.WebSocketRpcRequest)6 Feature (com.fasterxml.jackson.core.JsonGenerator.Feature)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)2 Jdk8Module (com.fasterxml.jackson.datatype.jdk8.Jdk8Module)2 Vertx (io.vertx.core.Vertx)2 ServerWebSocket (io.vertx.core.http.ServerWebSocket)2 DecodeException (io.vertx.core.json.DecodeException)2 JsonArray (io.vertx.core.json.JsonArray)2 JsonObject (io.vertx.core.json.JsonObject)2 User (io.vertx.ext.auth.User)2 IOException (java.io.IOException)2 List (java.util.List)2 Optional (java.util.Optional)2 IsAliveHandler (org.hyperledger.besu.ethereum.api.handlers.IsAliveHandler)2 UnsignedLongParameter (org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.UnsignedLongParameter)2 JsonRpcError (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError)2 INVALID_REQUEST (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError.INVALID_REQUEST)2 JsonRpcErrorResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse)2 JsonRpcResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse)2