use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse in project besu by hyperledger.
the class PrivGetEeaTransactionCountTest method validRequestProducesExpectedNonce.
@Test
public void validRequestProducesExpectedNonce() {
final long reportedNonce = 8L;
final PrivGetEeaTransactionCount method = new PrivGetEeaTransactionCount(privacyController, privacyIdProvider);
when(privacyController.determineNonce(any(), any(), eq(ENCLAVE_PUBLIC_KEY))).thenReturn(reportedNonce);
final JsonRpcResponse response = method.response(request);
assertThat(response).isInstanceOf(JsonRpcSuccessResponse.class);
final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response;
final int returnedValue = Integer.decode((String) successResponse.getResult());
assertThat(returnedValue).isEqualTo(reportedNonce);
}
use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse in project besu by hyperledger.
the class JsonRpcHttpService method handleJsonSingleRequest.
private void handleJsonSingleRequest(final RoutingContext routingContext, final JsonObject request, final Optional<User> user) {
final HttpServerResponse response = routingContext.response();
vertx.executeBlocking(future -> {
final JsonRpcResponse jsonRpcResponse = process(routingContext, request, user);
future.complete(jsonRpcResponse);
}, false, (res) -> {
if (!response.closed() && !response.headWritten()) {
if (res.failed()) {
response.setStatusCode(HttpResponseStatus.INTERNAL_SERVER_ERROR.code()).end();
return;
}
final JsonRpcResponse jsonRpcResponse = (JsonRpcResponse) res.result();
response.setStatusCode(status(jsonRpcResponse).code()).putHeader("Content-Type", APPLICATION_JSON);
if (jsonRpcResponse.getType() == JsonRpcResponseType.NONE) {
response.end(EMPTY_RESPONSE);
} else {
try {
// underlying output stream lifecycle is managed by the json object writer
JSON_OBJECT_WRITER.writeValue(new JsonResponseStreamer(response, routingContext.request().remoteAddress()), jsonRpcResponse);
} catch (IOException ex) {
LOG.error("Error streaming JSON-RPC response", ex);
}
}
}
});
}
use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse in project besu by hyperledger.
the class JsonRpcHttpService method handleJsonBatchRequest.
@SuppressWarnings("rawtypes")
private void handleJsonBatchRequest(final RoutingContext routingContext, final JsonArray jsonArray, final Optional<User> user) {
// Interpret json as rpc request
final List<Future> responses = jsonArray.stream().map(obj -> {
if (!(obj instanceof JsonObject)) {
return Future.succeededFuture(errorResponse(null, INVALID_REQUEST));
}
final JsonObject req = (JsonObject) obj;
return vertx.executeBlocking(future -> future.complete(process(routingContext, req, user)));
}).collect(toList());
CompositeFuture.all(responses).onComplete((res) -> {
final HttpServerResponse response = routingContext.response();
if (response.closed() || response.headWritten()) {
return;
}
if (res.failed()) {
response.setStatusCode(HttpResponseStatus.BAD_REQUEST.code()).end();
return;
}
final JsonRpcResponse[] completed = res.result().list().stream().map(JsonRpcResponse.class::cast).filter(this::isNonEmptyResponses).toArray(JsonRpcResponse[]::new);
try {
// underlying output stream lifecycle is managed by the json object writer
JSON_OBJECT_WRITER.writeValue(new JsonResponseStreamer(response, routingContext.request().remoteAddress()), completed);
} catch (IOException ex) {
LOG.error("Error streaming JSON-RPC response", ex);
}
});
}
use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse 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);
}
use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse in project besu by hyperledger.
the class QbftDiscardValidatorVoteTest method discardValidator.
@Test
public void discardValidator() {
final Address parameterAddress = Address.fromHexString("1");
final JsonRpcRequestContext request = requestWithParams(parameterAddress);
final JsonRpcResponse expectedResponse = new JsonRpcSuccessResponse(request.getRequest().getId(), true);
final JsonRpcResponse response = method.response(request);
assertThat(response).usingRecursiveComparison().isEqualTo(expectedResponse);
verify(voteProvider).discardVote(parameterAddress);
}
Aggregations