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));
}
}
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations