Skip to content

Commit

Permalink
5098 branch 19 update more invalid params (#7460)
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 authored Aug 16, 2024
1 parent cafe82d commit 68d7bd0
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcRequestException;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.JsonRpcParameter;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType;

import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -51,7 +52,8 @@ public JsonRpcRequest(
this.method = method;
this.params = params;
if (method == null) {
throw new InvalidJsonRpcRequestException("Field 'method' is required");
throw new InvalidJsonRpcRequestException(
"Field 'method' is required", RpcErrorType.INVALID_METHOD_PARAMS);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,13 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
throw new InvalidJsonRpcParameters(
"Invalid address hash parameter (index 2)", RpcErrorType.INVALID_ADDRESS_HASH_PARAMS, e);
}
final int maxResults = requestContext.getRequiredParameter(3, Integer.TYPE);
final int maxResults;
try {
maxResults = requestContext.getRequiredParameter(3, Integer.TYPE);
} catch (Exception e) { // TODO:replace with JsonRpcParameter.JsonRpcParameterException
throw new InvalidJsonRpcParameters(
"Invalid max results parameter (index 3)", RpcErrorType.INVALID_MAX_RESULTS_PARAMS, e);
}

final Optional<Hash> blockHashOptional = hashFromParameter(blockParameterOrBlockHash);
if (blockHashOptional.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.hyperledger.besu.datatypes.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 Down Expand Up @@ -49,12 +50,23 @@ public String getName() {
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
final Optional<PoWSolverInputs> solver = miner.getWorkDefinition();
if (solver.isPresent()) {
final PoWSolution solution =
new PoWSolution(
Bytes.fromHexString(requestContext.getRequiredParameter(0, String.class)).getLong(0),
requestContext.getRequiredParameter(2, Hash.class),
null,
Bytes.fromHexString(requestContext.getRequiredParameter(1, String.class)));
long nonce;
try {
nonce =
Bytes.fromHexString(requestContext.getRequiredParameter(0, String.class)).getLong(0);
} catch (Exception e) { // TODO:replace with JsonRpcParameter.JsonRpcParameterException
throw new InvalidJsonRpcParameters(
"Invalid nonce parameter (index 0)", RpcErrorType.INVALID_NONCE_PARAMS, e);
}
Hash mixHash;
try {
mixHash = requestContext.getRequiredParameter(2, Hash.class);
} catch (Exception e) { // TODO:replace with JsonRpcParameter.JsonRpcParameterException
throw new InvalidJsonRpcParameters(
"Invalid mix hash parameter (index 2)", RpcErrorType.INVALID_MIX_HASH_PARAMS, e);
}
Bytes powHash = Bytes.fromHexString(requestContext.getRequiredParameter(1, String.class));
final PoWSolution solution = new PoWSolution(nonce, mixHash, null, powHash);
final boolean result = miner.submitWork(solution);
return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), result);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,15 @@ public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext)

final Object reqId = requestContext.getRequest().getId();

Optional<String> maybeParentBeaconBlockRootParam =
requestContext.getOptionalParameter(2, String.class);
Optional<String> maybeParentBeaconBlockRootParam;
try {
maybeParentBeaconBlockRootParam = requestContext.getOptionalParameter(2, String.class);
} catch (Exception e) { // TODO:replace with JsonRpcParameter.JsonRpcParameterException
throw new InvalidJsonRpcRequestException(
"Invalid parent beacon block root parameters (index 2)",
RpcErrorType.INVALID_PARENT_BEACON_BLOCK_ROOT_PARAMS,
e);
}
final Optional<Bytes32> maybeParentBeaconBlockRoot =
maybeParentBeaconBlockRootParam.map(Bytes32::fromHexString);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ protected ValidationResult<RpcErrorType> validateParameters(
RpcErrorType.INVALID_PARAMS, "Missing versioned hashes field");
} else if (maybeBeaconBlockRootParam.isEmpty()) {
return ValidationResult.invalid(
RpcErrorType.INVALID_PARAMS, "Missing parent beacon block root field");
RpcErrorType.INVALID_PARENT_BEACON_BLOCK_ROOT_PARAMS,
"Missing parent beacon block root field");
} else {
return ValidationResult.valid();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ protected ValidationResult<RpcErrorType> validateParameters(
RpcErrorType.INVALID_PARAMS, "Missing versioned hashes field");
} else if (maybeBeaconBlockRootParam.isEmpty()) {
return ValidationResult.invalid(
RpcErrorType.INVALID_PARAMS, "Missing parent beacon block root field");
RpcErrorType.INVALID_PARENT_BEACON_BLOCK_ROOT_PARAMS,
"Missing parent beacon block root field");
} else if (payloadParameter.getDepositRequests() == null) {
return ValidationResult.invalid(RpcErrorType.INVALID_PARAMS, "Missing deposit field");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.hyperledger.besu.datatypes.Wei;
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.response.JsonRpcError;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
Expand Down Expand Up @@ -53,7 +54,13 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
} catch (final IllegalArgumentException invalidJsonRpcParameters) {
return new JsonRpcErrorResponse(
requestContext.getRequest().getId(),
new JsonRpcError(RpcErrorType.INVALID_PARAMS, invalidJsonRpcParameters.getMessage()));
new JsonRpcError(
RpcErrorType.INVALID_MIN_GAS_PRICE_PARAMS, invalidJsonRpcParameters.getMessage()));
} catch (Exception e) { // TODO:replace with JsonRpcParameter.JsonRpcParameterException
throw new InvalidJsonRpcParameters(
"Invalid min gas price parameter (index 0)",
RpcErrorType.INVALID_MIN_GAS_PRICE_PARAMS,
e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
} catch (final IllegalArgumentException invalidJsonRpcParameters) {
return new JsonRpcErrorResponse(
requestContext.getRequest().getId(),
new JsonRpcError(RpcErrorType.INVALID_PARAMS, invalidJsonRpcParameters.getMessage()));
new JsonRpcError(
RpcErrorType.INVALID_MIN_PRIORITY_FEE_PARAMS, invalidJsonRpcParameters.getMessage()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void shouldReturnFalseWhenParameterIsInvalid() {
new JsonRpcErrorResponse(
request.getRequest().getId(),
new JsonRpcError(
RpcErrorType.INVALID_PARAMS,
RpcErrorType.INVALID_MIN_GAS_PRICE_PARAMS,
"Illegal character '-' found at index 0 in hex binary representation"));

final JsonRpcResponse actual = method.response(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void shouldReturnInvalidParamsWhenParameterIsInvalid() {
new JsonRpcErrorResponse(
request.getRequest().getId(),
new JsonRpcError(
RpcErrorType.INVALID_PARAMS,
RpcErrorType.INVALID_MIN_PRIORITY_FEE_PARAMS,
"Hex value is too large: expected at most 32 bytes but got 33"));

final JsonRpcResponse actual = method.response(request);
Expand All @@ -65,7 +65,8 @@ public void shouldReturnInvalidParamsWhenParameterIsMissing() {
new JsonRpcErrorResponse(
request.getRequest().getId(),
new JsonRpcError(
RpcErrorType.INVALID_PARAMS, "Missing required json rpc parameter at index 0"));
RpcErrorType.INVALID_MIN_PRIORITY_FEE_PARAMS,
"Missing required json rpc parameter at index 0"));
final JsonRpcResponse actual = method.response(request);
assertThat(actual).usingRecursiveComparison().isEqualTo(expected);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcParameters;
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.blockcreation.MiningCoordinator;
import org.hyperledger.besu.ethereum.blockcreation.PoWMiningCoordinator;
import org.hyperledger.besu.ethereum.mainnet.DirectAcyclicGraphSeed;
Expand Down Expand Up @@ -170,13 +172,23 @@ public void handle(

private void handleMiningSubmit(final JsonRpcRequest message, final Consumer<String> sender) {
LOG.debug("Miner submitted solution {}", message);
long nonce;
try {
nonce = Bytes.fromHexString(message.getRequiredParameter(2, String.class)).getLong(0);
} catch (Exception e) {
throw new InvalidJsonRpcParameters(
"Invalid nonce parameter (index 2)", RpcErrorType.INVALID_NONCE_PARAMS, e);
}
Hash mixHash = null;
try {
mixHash = Hash.fromHexString(message.getRequiredParameter(4, String.class));
} catch (Exception e) { // TODO:replace with JsonRpcParameter.JsonRpcParameterException
throw new InvalidJsonRpcParameters(
"Invalid mix hash parameter (index 4)", RpcErrorType.INVALID_MIX_HASH_PARAMS, e);
}
Bytes powHash = Bytes.fromHexString(message.getRequiredParameter(3, String.class));
boolean result = false;
final PoWSolution solution =
new PoWSolution(
Bytes.fromHexString(message.getRequiredParameter(2, String.class)).getLong(0),
Hash.fromHexString(message.getRequiredParameter(4, String.class)),
null,
Bytes.fromHexString(message.getRequiredParameter(3, String.class)));
final PoWSolution solution = new PoWSolution(nonce, mixHash, null, powHash);
if (currentInput.getPrePowHash().equals(solution.getPowHash())) {
result = submitCallback.apply(solution);
}
Expand Down

0 comments on commit 68d7bd0

Please sign in to comment.