Skip to content

Commit

Permalink
Move JsonRpcResponseType to RpcResponseType in the plugin API mod…
Browse files Browse the repository at this point in the history
…ule (hyperledger#7392)

Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
  • Loading branch information
fab-10 authored Jul 29, 2024
1 parent 94b497e commit ab77523
Show file tree
Hide file tree
Showing 27 changed files with 93 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcParameters;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponseType;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.plugin.services.rpc.RpcResponseType;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -61,7 +61,7 @@ public void discardEmpty() {

final JsonRpcResponse response = discard.response(requestWithParams(a0));

assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS);
assertThat(response.getType()).isEqualTo(RpcResponseType.SUCCESS);
final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response;
assertThat(successResponse.getResult()).isEqualTo(true);
assertThat(validatorProvider.getVoteProviderAtHead().get().getProposals().get(a0)).isNull();
Expand All @@ -77,7 +77,7 @@ public void discardAuth() {
final JsonRpcResponse response = discard.response(requestWithParams(a0));

assertThat(validatorProvider.getVoteProviderAtHead().get().getProposals().get(a0)).isNull();
assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS);
assertThat(response.getType()).isEqualTo(RpcResponseType.SUCCESS);
final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response;
assertThat(successResponse.getResult()).isEqualTo(true);
}
Expand All @@ -92,7 +92,7 @@ public void discardDrop() {
final JsonRpcResponse response = discard.response(requestWithParams(a0));

assertThat(validatorProvider.getVoteProviderAtHead().get().getProposals().get(a0)).isNull();
assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS);
assertThat(response.getType()).isEqualTo(RpcResponseType.SUCCESS);
final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response;
assertThat(successResponse.getResult()).isEqualTo(true);
}
Expand All @@ -111,7 +111,7 @@ public void discardIsolation() {
assertThat(validatorProvider.getVoteProviderAtHead().get().getProposals().get(a0)).isNull();
assertThat(validatorProvider.getVoteProviderAtHead().get().getProposals().get(a1))
.isEqualTo(VoteType.ADD);
assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS);
assertThat(response.getType()).isEqualTo(RpcResponseType.SUCCESS);
final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response;
assertThat(successResponse.getResult()).isEqualTo(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponseType;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType;
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.plugin.services.rpc.RpcResponseType;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -59,7 +59,7 @@ public void testAuth() {

assertThat(validatorProvider.getVoteProviderAtHead().get().getProposals().get(a1))
.isEqualTo(VoteType.ADD);
assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS);
assertThat(response.getType()).isEqualTo(RpcResponseType.SUCCESS);
final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response;
assertThat(successResponse.getResult()).isEqualTo(true);
}
Expand All @@ -72,7 +72,7 @@ public void testAuthWithAddressZeroResultsInError() {
final JsonRpcResponse response = propose.response(requestWithParams(a0, true));

assertThat(validatorProvider.getVoteProviderAtHead().get().getProposals().get(a0)).isNull();
assertThat(response.getType()).isEqualTo(JsonRpcResponseType.ERROR);
assertThat(response.getType()).isEqualTo(RpcResponseType.ERROR);
final JsonRpcErrorResponse errorResponse = (JsonRpcErrorResponse) response;
assertThat(errorResponse.getErrorType()).isEqualTo(RpcErrorType.INVALID_REQUEST);
}
Expand All @@ -86,7 +86,7 @@ public void testDrop() {

assertThat(validatorProvider.getVoteProviderAtHead().get().getProposals().get(a1))
.isEqualTo(VoteType.DROP);
assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS);
assertThat(response.getType()).isEqualTo(RpcResponseType.SUCCESS);
final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response;
assertThat(successResponse.getResult()).isEqualTo(true);
}
Expand All @@ -99,7 +99,7 @@ public void testDropWithAddressZeroResultsInError() {
final JsonRpcResponse response = propose.response(requestWithParams(a0, false));

assertThat(validatorProvider.getVoteProviderAtHead().get().getProposals().get(a0)).isNull();
assertThat(response.getType()).isEqualTo(JsonRpcResponseType.ERROR);
assertThat(response.getType()).isEqualTo(RpcResponseType.ERROR);
final JsonRpcErrorResponse errorResponse = (JsonRpcErrorResponse) response;
assertThat(errorResponse.getErrorType()).isEqualTo(RpcErrorType.INVALID_REQUEST);
}
Expand All @@ -114,7 +114,7 @@ public void testRepeatAuth() {

assertThat(validatorProvider.getVoteProviderAtHead().get().getProposals().get(a1))
.isEqualTo(VoteType.ADD);
assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS);
assertThat(response.getType()).isEqualTo(RpcResponseType.SUCCESS);
final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response;
assertThat(successResponse.getResult()).isEqualTo(true);
}
Expand All @@ -129,7 +129,7 @@ public void testRepeatDrop() {

assertThat(validatorProvider.getVoteProviderAtHead().get().getProposals().get(a1))
.isEqualTo(VoteType.DROP);
assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS);
assertThat(response.getType()).isEqualTo(RpcResponseType.SUCCESS);
final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response;
assertThat(successResponse.getResult()).isEqualTo(true);
}
Expand All @@ -144,7 +144,7 @@ public void testChangeToAuth() {

assertThat(validatorProvider.getVoteProviderAtHead().get().getProposals().get(a1))
.isEqualTo(VoteType.ADD);
assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS);
assertThat(response.getType()).isEqualTo(RpcResponseType.SUCCESS);
final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response;
assertThat(successResponse.getResult()).isEqualTo(true);
}
Expand All @@ -159,7 +159,7 @@ public void testChangeToDrop() {

assertThat(validatorProvider.getVoteProviderAtHead().get().getProposals().get(a0))
.isEqualTo(VoteType.DROP);
assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS);
assertThat(response.getType()).isEqualTo(RpcResponseType.SUCCESS);
final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response;
assertThat(successResponse.getResult()).isEqualTo(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponseType;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.DebugTraceTransactionResult;
import org.hyperledger.besu.plugin.services.rpc.RpcResponseType;
import org.hyperledger.besu.testutil.BlockTestUtil;

import java.util.Map;
Expand Down Expand Up @@ -69,7 +69,7 @@ public void debugTraceTransactionSuccessTest() {
new JsonRpcRequestContext(new JsonRpcRequest("2.0", DEBUG_TRACE_TRANSACTION, params));

final JsonRpcResponse response = method.response(request);
assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS);
assertThat(response.getType()).isEqualTo(RpcResponseType.SUCCESS);
DebugTraceTransactionResult debugTraceTransactionResult =
(DebugTraceTransactionResult) ((JsonRpcSuccessResponse) response).getResult();
assertThat(debugTraceTransactionResult.getGas()).isEqualTo(23705L);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.execution.JsonRpcExecutor;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponseType;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType;
import org.hyperledger.besu.plugin.services.rpc.RpcResponseType;

import java.io.IOException;

Expand Down Expand Up @@ -74,7 +74,7 @@ public void executeRpcRequestBatch(
generator.writeStartArray();
for (int i = 0; i < rpcRequestBatch.size(); i++) {
JsonRpcResponse response = processMaybeRequest(rpcRequestBatch.getValue(i));
if (response.getType() != JsonRpcResponseType.NONE) {
if (response.getType() != RpcResponseType.NONE) {
generator.writeObject(response);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.execution.JsonRpcExecutor;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponseType;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType;
import org.hyperledger.besu.plugin.services.rpc.RpcResponseType;

import java.io.IOException;

Expand Down Expand Up @@ -70,7 +70,7 @@ private void handleJsonObjectResponse(
final RoutingContext ctx)
throws IOException {
response.setStatusCode(status(jsonRpcResponse).code());
if (jsonRpcResponse.getType() == JsonRpcResponseType.NONE) {
if (jsonRpcResponse.getType() == RpcResponseType.NONE) {
response.end();
} else {
try (final JsonResponseStreamer streamer =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponseType;
import org.hyperledger.besu.metrics.BesuMetricCategory;
import org.hyperledger.besu.plugin.services.MetricsSystem;
import org.hyperledger.besu.plugin.services.metrics.Counter;
import org.hyperledger.besu.plugin.services.metrics.LabelledMetric;
import org.hyperledger.besu.plugin.services.rpc.RpcResponseType;

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.StatusCode;
Expand Down Expand Up @@ -52,7 +52,7 @@ public JsonRpcResponse process(
final Span metricSpan,
final JsonRpcRequestContext request) {
JsonRpcResponse jsonRpcResponse = rpcProcessor.process(id, method, metricSpan, request);
if (JsonRpcResponseType.ERROR == jsonRpcResponse.getType()) {
if (RpcResponseType.ERROR == jsonRpcResponse.getType()) {
JsonRpcErrorResponse errorResponse = (JsonRpcErrorResponse) jsonRpcResponse;
this.rpcErrorsCounter.labels(method.getName(), errorResponse.getErrorType().name()).inc();
switch (errorResponse.getErrorType()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.response;

import org.hyperledger.besu.ethereum.mainnet.ValidationResult;
import org.hyperledger.besu.plugin.services.rpc.RpcResponseType;

import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -72,8 +73,8 @@ public JsonRpcError getError() {

@Override
@JsonIgnore
public JsonRpcResponseType getType() {
return JsonRpcResponseType.ERROR;
public RpcResponseType getType() {
return RpcResponseType.ERROR;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
*/
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.response;

import org.hyperledger.besu.plugin.services.rpc.RpcResponseType;

public class JsonRpcNoResponse implements JsonRpcResponse {

@Override
public JsonRpcResponseType getType() {
return JsonRpcResponseType.NONE;
public RpcResponseType getType() {
return RpcResponseType.NONE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.response;

import org.hyperledger.besu.plugin.services.rpc.RpcResponseType;

import com.fasterxml.jackson.annotation.JsonGetter;

public interface JsonRpcResponse {
Expand All @@ -23,5 +25,5 @@ default String getVersion() {
return "2.0";
}

JsonRpcResponseType getType();
RpcResponseType getType();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.response;

import org.hyperledger.besu.plugin.services.rpc.RpcResponseType;

import java.util.Objects;

import com.fasterxml.jackson.annotation.JsonGetter;
Expand Down Expand Up @@ -50,8 +52,8 @@ public Object getResult() {

@Override
@JsonIgnore
public JsonRpcResponseType getType() {
return JsonRpcResponseType.SUCCESS;
public RpcResponseType getType() {
return RpcResponseType.SUCCESS;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.response;

import org.hyperledger.besu.plugin.services.rpc.RpcResponseType;

import java.util.Arrays;
import java.util.Objects;

Expand Down Expand Up @@ -50,8 +52,8 @@ public JsonRpcError getError() {

@Override
@JsonIgnore
public JsonRpcResponseType getType() {
return JsonRpcResponseType.UNAUTHORIZED;
public RpcResponseType getType() {
return RpcResponseType.UNAUTHORIZED;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponseType;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType;
import org.hyperledger.besu.plugin.services.rpc.RpcResponseType;

import java.io.IOException;
import java.nio.file.Files;
Expand Down Expand Up @@ -155,7 +155,7 @@ public Future<NetServer> start() {
.filter(
jsonRpcResponse ->
jsonRpcResponse.getType()
!= JsonRpcResponseType.NONE)
!= RpcResponseType.NONE)
.toArray(JsonRpcResponse[]::new);

socket.write(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.execution.JsonRpcExecutor;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponseType;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType;
import org.hyperledger.besu.ethereum.api.jsonrpc.websocket.methods.WebSocketRpcRequest;
import org.hyperledger.besu.ethereum.eth.manager.EthScheduler;
import org.hyperledger.besu.plugin.services.rpc.RpcResponseType;

import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -151,7 +151,7 @@ public void handle(
jsonRpcBatchResponse.stream()
.filter(
jsonRpcResponse ->
jsonRpcResponse.getType() != JsonRpcResponseType.NONE)
jsonRpcResponse.getType() != RpcResponseType.NONE)
.toArray(JsonRpcResponse[]::new);
replyToClient(websocket, completed);
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcParameters;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponseType;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BlockResult;
Expand All @@ -41,6 +40,7 @@
import org.hyperledger.besu.ethereum.core.TransactionReceipt;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive;
import org.hyperledger.besu.plugin.services.rpc.RpcResponseType;

import java.util.List;

Expand Down Expand Up @@ -146,15 +146,15 @@ public void exceptionWhenBoolParamInvalid() {
@Test
public void errorWhenAskingFinalizedButFinalizedIsNotPresent() {
JsonRpcResponse resp = method.response(requestWithParams("finalized", "false"));
assertThat(resp.getType()).isEqualTo(JsonRpcResponseType.ERROR);
assertThat(resp.getType()).isEqualTo(RpcResponseType.ERROR);
JsonRpcErrorResponse errorResp = (JsonRpcErrorResponse) resp;
assertThat(errorResp.getErrorType()).isEqualTo(RpcErrorType.UNKNOWN_BLOCK);
}

@Test
public void errorWhenAskingSafeButSafeIsNotPresent() {
JsonRpcResponse resp = method.response(requestWithParams("safe", "false"));
assertThat(resp.getType()).isEqualTo(JsonRpcResponseType.ERROR);
assertThat(resp.getType()).isEqualTo(RpcResponseType.ERROR);
JsonRpcErrorResponse errorResp = (JsonRpcErrorResponse) resp;
assertThat(errorResp.getErrorType()).isEqualTo(RpcErrorType.UNKNOWN_BLOCK);
}
Expand All @@ -181,7 +181,7 @@ public void successWhenAskingSafe() {

private void assertSuccess(final String tag, final long height) {
JsonRpcResponse resp = method.response(requestWithParams(tag, "false"));
assertThat(resp.getType()).isEqualTo(JsonRpcResponseType.SUCCESS);
assertThat(resp.getType()).isEqualTo(RpcResponseType.SUCCESS);
JsonRpcSuccessResponse successResp = (JsonRpcSuccessResponse) resp;
BlockResult blockResult = (BlockResult) successResp.getResult();
assertThat(blockResult.getHash())
Expand Down
Loading

0 comments on commit ab77523

Please sign in to comment.