Skip to content

Commit

Permalink
5098 branch 13 update invalid call consolidation and privacy group (h…
Browse files Browse the repository at this point in the history
…yperledger#7441)

* 5098: Add RpcErrorTypes

Signed-off-by: Matilda Clerke <matilda.clerke@consensys.net>

---------

Signed-off-by: Matilda Clerke <matilda.clerke@consensys.net>
Signed-off-by: Matilda-Clerke <matilda.clerke@consensys.net>
Co-authored-by: Sally MacFarlane <macfarla.github@gmail.com>
Signed-off-by: gconnect <agatevureglory@gmail.com>
  • Loading branch information
2 people authored and gconnect committed Aug 26, 2024
1 parent 33dd5e7 commit 2ad1917
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,20 @@
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.parameters.JsonCallParameter;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType;

public class JsonCallParameterUtil {

private JsonCallParameterUtil() {}

public static JsonCallParameter validateAndGetCallParams(final JsonRpcRequestContext request) {
final JsonCallParameter callParams = request.getRequiredParameter(0, JsonCallParameter.class);
final JsonCallParameter callParams;
try {
callParams = request.getRequiredParameter(0, JsonCallParameter.class);
} catch (Exception e) { // TODO:replace with JsonRpcParameter.JsonRpcParameterException
throw new InvalidJsonRpcParameters(
"Invalid call parameters (index 0)", RpcErrorType.INVALID_CALL_PARAMS);
}

if (callParams.getGasPrice() != null
&& (callParams.getMaxFeePerGas().isPresent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,7 @@ public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext)
if (!getConsolidationRequestValidator(
protocolSchedule.get(), blockParam.getTimestamp(), blockParam.getBlockNumber())
.validateParameter(maybeConsolidationRequests)) {
return new JsonRpcErrorResponse(
reqId, new JsonRpcError(INVALID_PARAMS, "Invalid consolidation request"));
return new JsonRpcErrorResponse(reqId, RpcErrorType.INVALID_CONSOLIDATION_REQUEST_PARAMS);
}

Optional<List<Request>> maybeRequests =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,16 @@ private JsonRpcError errorResponse(
}

private JsonCallParameter validateAndGetCallParams(final JsonRpcRequestContext request) {
final JsonCallParameter callParams = request.getRequiredParameter(1, JsonCallParameter.class);
final JsonCallParameter callParams;
try {
callParams = request.getRequiredParameter(1, JsonCallParameter.class);
} catch (Exception e) { // TODO:replace with JsonRpcParameter.JsonRpcParameterException
throw new InvalidJsonRpcParameters(
"Invalid call parameters (index 1)", RpcErrorType.INVALID_CALL_PARAMS);
}
if (callParams.getTo() == null) {
throw new InvalidJsonRpcParameters("Missing \"to\" field in call arguments");
throw new InvalidJsonRpcParameters(
"Missing \"to\" field in call arguments", RpcErrorType.INVALID_CALL_PARAMS);
}
return callParams;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcEnclaveErrorConverter;
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod;
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.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.PrivacyIdProvider;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.parameters.CreatePrivacyGroupParameter;
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.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType;
import org.hyperledger.besu.ethereum.privacy.PrivacyController;

import org.slf4j.Logger;
Expand All @@ -50,8 +52,15 @@ public String getName() {
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
LOG.trace("Executing {}", RpcMethod.PRIV_CREATE_PRIVACY_GROUP.getMethodName());

final CreatePrivacyGroupParameter parameter =
requestContext.getRequiredParameter(0, CreatePrivacyGroupParameter.class);
final CreatePrivacyGroupParameter parameter;
try {
parameter = requestContext.getRequiredParameter(0, CreatePrivacyGroupParameter.class);
} catch (Exception e) { // TODO:replace with JsonRpcParameter.JsonRpcParameterException
throw new InvalidJsonRpcParameters(
"Invalid create privacy group parameter (index 0)",
RpcErrorType.INVALID_CREATE_PRIVACY_GROUP_PARAMS,
e);
}

LOG.trace(
"Creating a privacy group with name {} and description {}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public String getDescription() {
catchThrowableOfType(
() -> privCreatePrivacyGroup.response(request), InvalidJsonRpcParameters.class);

assertThat(response.getMessage()).contains("Invalid json rpc parameter at index 0");
assertThat(response.getMessage()).contains("Invalid create privacy group parameter (index 0)");
}

@Test
Expand All @@ -249,7 +249,7 @@ public void returnsCorrectExceptionMissingParam() {
catchThrowableOfType(
() -> privCreatePrivacyGroup.response(request), InvalidJsonRpcParameters.class);

assertThat(response.getMessage()).isEqualTo("Missing required json rpc parameter at index 0");
assertThat(response.getMessage()).isEqualTo("Invalid create privacy group parameter (index 0)");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"response": {
"jsonrpc": "2.0",
"id": 4,
"error":{"code":-32602,"message":"Invalid params"}
"error":{"code":-32602,"message":"Invalid call params"}
},
"statusCode": 200
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"id": 3,
"error" : {
"code" : -32602,
"message" : "Invalid params"
"message" : "Invalid call params"
}
},
"statusCode": 200
Expand Down

0 comments on commit 2ad1917

Please sign in to comment.