From 68d7bd0bd1d9681a3334e2bdc41c6e11f8381697 Mon Sep 17 00:00:00 2001 From: Matilda-Clerke Date: Fri, 16 Aug 2024 11:59:57 +1000 Subject: [PATCH] 5098 branch 19 update more invalid params (#7460) * 5098: Add RpcErrorTypes Signed-off-by: Matilda Clerke --------- Signed-off-by: Matilda Clerke Signed-off-by: Matilda-Clerke Co-authored-by: Sally MacFarlane --- .../api/jsonrpc/internal/JsonRpcRequest.java | 4 +++- .../internal/methods/DebugAccountRange.java | 8 ++++++- .../internal/methods/EthSubmitWork.java | 24 ++++++++++++++----- .../engine/AbstractEngineNewPayload.java | 11 +++++++-- .../methods/engine/EngineNewPayloadV3.java | 3 ++- .../methods/engine/EngineNewPayloadV4.java | 3 ++- .../methods/miner/MinerSetMinGasPrice.java | 9 ++++++- .../methods/miner/MinerSetMinPriorityFee.java | 3 ++- .../miner/MinerSetMinGasPriceTest.java | 2 +- .../miner/MinerSetMinPriorityFeeTest.java | 5 ++-- .../ethereum/stratum/Stratum1Protocol.java | 24 ++++++++++++++----- 11 files changed, 73 insertions(+), 23 deletions(-) diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/JsonRpcRequest.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/JsonRpcRequest.java index db7fe13105..06b495624e 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/JsonRpcRequest.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/JsonRpcRequest.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); } } diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/DebugAccountRange.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/DebugAccountRange.java index c891bde56a..fc787031da 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/DebugAccountRange.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/DebugAccountRange.java @@ -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 blockHashOptional = hashFromParameter(blockParameterOrBlockHash); if (blockHashOptional.isEmpty()) { diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthSubmitWork.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthSubmitWork.java index 370643f203..cebb2e7a00 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthSubmitWork.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthSubmitWork.java @@ -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 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 { diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/engine/AbstractEngineNewPayload.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/engine/AbstractEngineNewPayload.java index 0588121516..369d7a387a 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/engine/AbstractEngineNewPayload.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/engine/AbstractEngineNewPayload.java @@ -123,8 +123,15 @@ public abstract class AbstractEngineNewPayload extends ExecutionEngineJsonRpcMet final Object reqId = requestContext.getRequest().getId(); - Optional maybeParentBeaconBlockRootParam = - requestContext.getOptionalParameter(2, String.class); + Optional 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 maybeParentBeaconBlockRoot = maybeParentBeaconBlockRootParam.map(Bytes32::fromHexString); diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/engine/EngineNewPayloadV3.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/engine/EngineNewPayloadV3.java index b946df40bd..0277d6990e 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/engine/EngineNewPayloadV3.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/engine/EngineNewPayloadV3.java @@ -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(); } diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/engine/EngineNewPayloadV4.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/engine/EngineNewPayloadV4.java index b87eceed5d..a767c72345 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/engine/EngineNewPayloadV4.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/engine/EngineNewPayloadV4.java @@ -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 { diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/miner/MinerSetMinGasPrice.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/miner/MinerSetMinGasPrice.java index c72e5c95c7..59c18b80db 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/miner/MinerSetMinGasPrice.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/miner/MinerSetMinGasPrice.java @@ -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); } } } diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/miner/MinerSetMinPriorityFee.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/miner/MinerSetMinPriorityFee.java index f3dd52f906..9c217b0c23 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/miner/MinerSetMinPriorityFee.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/miner/MinerSetMinPriorityFee.java @@ -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())); } } } diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/miner/MinerSetMinGasPriceTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/miner/MinerSetMinGasPriceTest.java index a5cb18a2df..fb56b57307 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/miner/MinerSetMinGasPriceTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/miner/MinerSetMinGasPriceTest.java @@ -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); diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/miner/MinerSetMinPriorityFeeTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/miner/MinerSetMinPriorityFeeTest.java index cc69c1e7c4..7e564ac425 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/miner/MinerSetMinPriorityFeeTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/miner/MinerSetMinPriorityFeeTest.java @@ -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); } diff --git a/ethereum/stratum/src/main/java/org/hyperledger/besu/ethereum/stratum/Stratum1Protocol.java b/ethereum/stratum/src/main/java/org/hyperledger/besu/ethereum/stratum/Stratum1Protocol.java index d3408dc4c9..308d15f6f1 100644 --- a/ethereum/stratum/src/main/java/org/hyperledger/besu/ethereum/stratum/Stratum1Protocol.java +++ b/ethereum/stratum/src/main/java/org/hyperledger/besu/ethereum/stratum/Stratum1Protocol.java @@ -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 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); }