Skip to content

Commit

Permalink
5098 branch 14 update invalid data deposit and engine exchange (#7442)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
Matilda-Clerke and macfarla committed Aug 13, 2024
1 parent b8c062c commit 1a91545
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.hyperledger.besu.crypto.Hash;
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.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
Expand All @@ -41,11 +42,17 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
requestContext.getRequest().getId(), RpcErrorType.INVALID_PARAMS);
}

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

if (!data.isEmpty() && !data.startsWith("0x")) {
return new JsonRpcErrorResponse(
requestContext.getRequest().getId(), RpcErrorType.INVALID_PARAMS);
requestContext.getRequest().getId(), RpcErrorType.INVALID_DATA_PARAMS);
}

try {
Expand All @@ -54,7 +61,7 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
requestContext.getRequest().getId(), Hash.keccak256(byteData).toString());
} catch (final IllegalArgumentException err) {
return new JsonRpcErrorResponse(
requestContext.getRequest().getId(), RpcErrorType.INVALID_PARAMS);
requestContext.getRequest().getId(), RpcErrorType.INVALID_DATA_PARAMS);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,7 @@ public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext)
if (!getDepositRequestValidator(
protocolSchedule.get(), blockParam.getTimestamp(), blockParam.getBlockNumber())
.validateParameter(maybeDepositRequests)) {
return new JsonRpcErrorResponse(
reqId, new JsonRpcError(INVALID_PARAMS, "Invalid deposit request"));
return new JsonRpcErrorResponse(reqId, RpcErrorType.INVALID_DEPOSIT_REQUEST_PARAMS);
}

final Optional<List<Request>> maybeWithdrawalRequests =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.ProtocolContext;
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.ExecutionEngineJsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.EngineExchangeTransitionConfigurationParameter;
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.api.jsonrpc.internal.results.EngineExchangeTransitionConfigurationResult;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.Difficulty;
Expand Down Expand Up @@ -65,9 +67,17 @@ public String getName() {
public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext) {
engineCallListener.executionEngineCalled();

final EngineExchangeTransitionConfigurationParameter remoteTransitionConfiguration =
requestContext.getRequiredParameter(
0, EngineExchangeTransitionConfigurationParameter.class);
final EngineExchangeTransitionConfigurationParameter remoteTransitionConfiguration;
try {
remoteTransitionConfiguration =
requestContext.getRequiredParameter(
0, EngineExchangeTransitionConfigurationParameter.class);
} catch (Exception e) { // TODO:replace with JsonRpcParameter.JsonRpcParameterException
throw new InvalidJsonRpcParameters(
"Invalid engine exchange transition configuration parameters (index 0)",
RpcErrorType.INVALID_ENGINE_EXCHANGE_TRANSITION_CONFIGURATION_PARAMS,
e);
}
final Object reqId = requestContext.getRequest().getId();

LOG.atTrace()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void shouldReturnErrorOnOddLengthParam() {
new JsonRpcRequest("2", "web3_sha3", new Object[] {"0x68656c6c6f20776f726c6"}));

final JsonRpcResponse expected =
new JsonRpcErrorResponse(request.getRequest().getId(), RpcErrorType.INVALID_PARAMS);
new JsonRpcErrorResponse(request.getRequest().getId(), RpcErrorType.INVALID_DATA_PARAMS);
final JsonRpcResponse actual = method.response(request);

assertThat(actual).usingRecursiveComparison().isEqualTo(expected);
Expand All @@ -83,7 +83,7 @@ public void shouldReturnErrorOnNonHexParam() {
new JsonRpcRequest("2", "web3_sha3", new Object[] {"0x68656c6c6fThisIsNotHex"}));

final JsonRpcResponse expected =
new JsonRpcErrorResponse(request.getRequest().getId(), RpcErrorType.INVALID_PARAMS);
new JsonRpcErrorResponse(request.getRequest().getId(), RpcErrorType.INVALID_DATA_PARAMS);
final JsonRpcResponse actual = method.response(request);

assertThat(actual).usingRecursiveComparison().isEqualTo(expected);
Expand All @@ -96,7 +96,7 @@ public void shouldReturnErrorOnNoPrefixParam() {
new JsonRpcRequest("2", "web3_sha3", new Object[] {"68656c6c6f20776f726c64"}));

final JsonRpcResponse expected =
new JsonRpcErrorResponse(request.getRequest().getId(), RpcErrorType.INVALID_PARAMS);
new JsonRpcErrorResponse(request.getRequest().getId(), RpcErrorType.INVALID_DATA_PARAMS);
final JsonRpcResponse actual = method.response(request);

assertThat(actual).usingRecursiveComparison().isEqualTo(expected);
Expand All @@ -109,7 +109,7 @@ public void shouldReturnErrorOnNoPrefixNonHexParam() {
new JsonRpcRequest("2", "web3_sha3", new Object[] {"68656c6c6fThisIsNotHex"}));

final JsonRpcResponse expected =
new JsonRpcErrorResponse(request.getRequest().getId(), RpcErrorType.INVALID_PARAMS);
new JsonRpcErrorResponse(request.getRequest().getId(), RpcErrorType.INVALID_DATA_PARAMS);
final JsonRpcResponse actual = method.response(request);

assertThat(actual).usingRecursiveComparison().isEqualTo(expected);
Expand Down

0 comments on commit 1a91545

Please sign in to comment.