5098 branch 19 update more invalid params (#7460)

* 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>
pull/7473/head
Matilda-Clerke 3 months ago committed by GitHub
parent cafe82d2d5
commit 68d7bd0bd1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 4
      ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/JsonRpcRequest.java
  2. 8
      ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/DebugAccountRange.java
  3. 24
      ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthSubmitWork.java
  4. 11
      ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/engine/AbstractEngineNewPayload.java
  5. 3
      ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/engine/EngineNewPayloadV3.java
  6. 3
      ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/engine/EngineNewPayloadV4.java
  7. 9
      ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/miner/MinerSetMinGasPrice.java
  8. 3
      ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/miner/MinerSetMinPriorityFee.java
  9. 2
      ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/miner/MinerSetMinGasPriceTest.java
  10. 5
      ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/miner/MinerSetMinPriorityFeeTest.java
  11. 24
      ethereum/stratum/src/main/java/org/hyperledger/besu/ethereum/stratum/Stratum1Protocol.java

@ -16,6 +16,7 @@ package org.hyperledger.besu.ethereum.api.jsonrpc.internal;
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;
@ -51,7 +52,8 @@ public class 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);
}
}

@ -75,7 +75,13 @@ public class DebugAccountRange implements JsonRpcMethod {
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()) {

@ -17,6 +17,7 @@ package org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods;
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;
@ -49,12 +50,23 @@ public class EthSubmitWork implements JsonRpcMethod {
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 {

@ -123,8 +123,15 @@ public abstract class AbstractEngineNewPayload extends ExecutionEngineJsonRpcMet
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);

@ -67,7 +67,8 @@ public class EngineNewPayloadV3 extends AbstractEngineNewPayload {
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();
}

@ -67,7 +67,8 @@ public class EngineNewPayloadV4 extends AbstractEngineNewPayload {
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 {

@ -17,6 +17,7 @@ package org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.miner;
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;
@ -53,7 +54,13 @@ public class MinerSetMinGasPrice implements JsonRpcMethod {
} 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);
}
}
}

@ -54,7 +54,8 @@ public class MinerSetMinPriorityFee implements JsonRpcMethod {
} 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()));
}
}
}

@ -48,7 +48,7 @@ public class MinerSetMinGasPriceTest {
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);

@ -49,7 +49,7 @@ public class MinerSetMinPriorityFeeTest {
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);
@ -65,7 +65,8 @@ public class MinerSetMinPriorityFeeTest {
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);
}

@ -17,7 +17,9 @@ package org.hyperledger.besu.ethereum.stratum;
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;
@ -170,13 +172,23 @@ public class Stratum1Protocol implements StratumProtocol {
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);
}

Loading…
Cancel
Save