Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5098: Add additional RpcErrorTypes for invalid parameters #7389

Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ public void shouldFailToSendToToStrictNodeWithoutChainId() {
@Test
public void shouldFailToSendWithInvalidRlp() {
final String invalidRawTx = "0x5555";
strictNode.verify(eth.expectEthSendRawTransactionException(invalidRawTx, "Invalid params"));
strictNode.verify(
eth.expectEthSendRawTransactionException(
invalidRawTx, "Invalid transaction params (missing or incorrect)"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
import org.hyperledger.besu.consensus.common.validator.ValidatorProvider;
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.parameters.BlockParameter;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.JsonRpcParameter;
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 @@ -67,8 +69,13 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
}

private Optional<BlockHeader> determineBlockHeader(final JsonRpcRequestContext request) {
final Optional<BlockParameter> blockParameter =
request.getOptionalParameter(0, BlockParameter.class);
final Optional<BlockParameter> blockParameter;
try {
blockParameter = request.getOptionalParameter(0, BlockParameter.class);
} catch (JsonRpcParameter.JsonRpcParameterException e) {
throw new InvalidJsonRpcParameters(
"Invalid block parameter", RpcErrorType.INVALID_BLOCK_PARAMS, e);
}
final long latest = blockchainQueries.headBlockNumber();
final long blockNumber = blockParameter.map(b -> b.getNumber().orElse(latest)).orElse(latest);
return blockchainQueries.blockByNumber(blockNumber).map(BlockWithMetadata::getHeader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
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.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.JsonRpcParameter;
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 @@ -67,7 +69,13 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
}

private Optional<BlockHeader> determineBlockHeader(final JsonRpcRequestContext request) {
final Hash hash = request.getRequiredParameter(0, Hash.class);
final Hash hash;
try {
hash = request.getRequiredParameter(0, Hash.class);
} catch (JsonRpcParameter.JsonRpcParameterException e) {
throw new InvalidJsonRpcParameters(
"Invalid block hash parameter", RpcErrorType.INVALID_BLOCK_HASH_PARAMS, e);
}
return blockchainQueries.blockByHash(hash).map(BlockWithMetadata::getHeader);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
import org.hyperledger.besu.datatypes.Address;
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.parameters.JsonRpcParameter;
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;

/** The Discard Json RPC method. */
public class Discard implements JsonRpcMethod {
Expand All @@ -46,7 +49,13 @@ public String getName() {
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
checkState(
validatorProvider.getVoteProviderAtHead().isPresent(), "Clique requires a vote provider");
final Address address = requestContext.getRequiredParameter(0, Address.class);
final Address address;
try {
address = requestContext.getRequiredParameter(0, Address.class);
} catch (JsonRpcParameter.JsonRpcParameterException e) {
throw new InvalidJsonRpcParameters(
"Invalid address parameter", RpcErrorType.INVALID_ADDRESS_PARAMS, e);
}
validatorProvider.getVoteProviderAtHead().get().discardVote(address);
return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import org.hyperledger.besu.datatypes.Address;
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.parameters.JsonRpcParameter;
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,8 +51,20 @@ public String getName() {
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
checkState(
validatorProvider.getVoteProviderAtHead().isPresent(), "Clique requires a vote provider");
final Address address = requestContext.getRequiredParameter(0, Address.class);
final Boolean auth = requestContext.getRequiredParameter(1, Boolean.class);
final Address address;
try {
address = requestContext.getRequiredParameter(0, Address.class);
} catch (JsonRpcParameter.JsonRpcParameterException e) {
throw new InvalidJsonRpcParameters(
"Invalid address parameter", RpcErrorType.INVALID_ADDRESS_PARAMS, e);
}
final Boolean auth;
try {
auth = requestContext.getRequiredParameter(1, Boolean.class);
} catch (JsonRpcParameter.JsonRpcParameterException e) {
throw new InvalidJsonRpcParameters(
"Invalid auth parameter", RpcErrorType.INVALID_AUTH_PARAMS, e);
}
if (address.equals(CliqueBlockInterface.NO_VOTE_SUBJECT)) {
return new JsonRpcErrorResponse(
requestContext.getRequest().getId(), RpcErrorType.INVALID_REQUEST);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ public void returnsCorrectMethodName() {
public void exceptionWhenInvalidStartBlockSupplied() {
assertThatThrownBy(() -> method.response(requestWithParams("INVALID")))
.isInstanceOf(InvalidJsonRpcParameters.class)
.hasMessageContaining("Invalid json rpc parameter at index 0");
.hasMessageContaining("Invalid start block parameter");
}

@Test
public void exceptionWhenInvalidEndBlockSupplied() {
assertThatThrownBy(() -> method.response(requestWithParams("1", "INVALID")))
.isInstanceOf(InvalidJsonRpcParameters.class)
.hasMessageContaining("Invalid json rpc parameter at index 1");
.hasMessageContaining("Invalid end block parameter");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void failsWhenNoParam() {

assertThat(thrown)
.isInstanceOf(InvalidJsonRpcParameters.class)
.hasMessage("Missing required json rpc parameter at index 0");
.hasMessage("Invalid block hash parameter");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void discardWithoutAddress() {
final Discard discard = new Discard(validatorProvider);

assertThatThrownBy(() -> discard.response(requestWithParams()))
.hasMessage("Missing required json rpc parameter at index 0")
.hasMessage("Invalid address parameter")
.isInstanceOf(InvalidJsonRpcParameters.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import org.hyperledger.besu.consensus.common.validator.ValidatorProvider;
import org.hyperledger.besu.datatypes.Address;
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.BlockParameter;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.JsonRpcParameter;
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 @@ -66,17 +68,27 @@ protected AbstractGetSignerMetricsMethod(
*/
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {

final Optional<BlockParameter> startBlockParameter =
requestContext.getOptionalParameter(0, BlockParameter.class);
final Optional<BlockParameter> endBlockParameter =
requestContext.getOptionalParameter(1, BlockParameter.class);
final Optional<BlockParameter> startBlockParameter;
try {
startBlockParameter = requestContext.getOptionalParameter(0, BlockParameter.class);
} catch (JsonRpcParameter.JsonRpcParameterException e) {
throw new InvalidJsonRpcParameters(
"Invalid start block parameter", RpcErrorType.INVALID_BLOCK_NUMBER_PARAMS, e);
}
final Optional<BlockParameter> endBlockParameter;
try {
endBlockParameter = requestContext.getOptionalParameter(1, BlockParameter.class);
} catch (JsonRpcParameter.JsonRpcParameterException e) {
throw new InvalidJsonRpcParameters(
"Invalid end block parameter", RpcErrorType.INVALID_BLOCK_NUMBER_PARAMS, e);
}

final long fromBlockNumber = getFromBlockNumber(startBlockParameter);
final long toBlockNumber = getEndBlockNumber(endBlockParameter);

if (!isValidParameters(fromBlockNumber, toBlockNumber)) {
return new JsonRpcErrorResponse(
requestContext.getRequest().getId(), RpcErrorType.INVALID_PARAMS);
requestContext.getRequest().getId(), RpcErrorType.INVALID_BLOCK_NUMBER_PARAMS);
}

final Map<Address, SignerMetricResult> proposersMap = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
import org.hyperledger.besu.datatypes.Address;
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.parameters.JsonRpcParameter;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -50,7 +53,13 @@ public String getName() {
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
checkState(
validatorProvider.getVoteProviderAtHead().isPresent(), "Ibft requires a vote provider");
final Address validatorAddress = requestContext.getRequiredParameter(0, Address.class);
final Address validatorAddress;
try {
validatorAddress = requestContext.getRequiredParameter(0, Address.class);
} catch (JsonRpcParameter.JsonRpcParameterException e) {
throw new InvalidJsonRpcParameters(
"Invalid validator address parameter", RpcErrorType.INVALID_ADDRESS_PARAMS, e);
}
LOG.trace("Received RPC rpcName={} address={}", getName(), validatorAddress);
validatorProvider.getVoteProviderAtHead().get().discardVote(validatorAddress);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@
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.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.JsonRpcParameter;
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.chain.Blockchain;
import org.hyperledger.besu.ethereum.core.BlockHeader;

Expand Down Expand Up @@ -61,7 +64,13 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
}

private Object blockResult(final JsonRpcRequestContext request) {
final Hash hash = request.getRequiredParameter(0, Hash.class);
final Hash hash;
try {
hash = request.getRequiredParameter(0, Hash.class);
} catch (JsonRpcParameter.JsonRpcParameterException e) {
throw new InvalidJsonRpcParameters(
"Invalid block hash parameter", RpcErrorType.INVALID_BLOCK_HASH_PARAMS, e);
}
LOG.trace("Received RPC rpcName={} blockHash={}", getName(), hash);
final Optional<BlockHeader> blockHeader = blockchain.getBlockHeader(hash);
return blockHeader
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
import org.hyperledger.besu.consensus.common.BlockInterface;
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.AbstractBlockParameterMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameter;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.JsonRpcParameter;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType;
import org.hyperledger.besu.ethereum.api.query.BlockchainQueries;
import org.hyperledger.besu.ethereum.core.BlockHeader;

Expand Down Expand Up @@ -50,7 +53,12 @@ public IbftGetValidatorsByBlockNumber(

@Override
protected BlockParameter blockParameter(final JsonRpcRequestContext request) {
return request.getRequiredParameter(0, BlockParameter.class);
try {
return request.getRequiredParameter(0, BlockParameter.class);
} catch (JsonRpcParameter.JsonRpcParameterException e) {
throw new InvalidJsonRpcParameters(
"Invalid block parameter", RpcErrorType.INVALID_BLOCK_PARAMS, e);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@
import org.hyperledger.besu.datatypes.Address;
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.parameters.JsonRpcParameter;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -51,8 +54,20 @@ public String getName() {
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
checkState(
validatorProvider.getVoteProviderAtHead().isPresent(), "Ibft requires a vote provider");
final Address validatorAddress = requestContext.getRequiredParameter(0, Address.class);
final Boolean add = requestContext.getRequiredParameter(1, Boolean.class);
final Address validatorAddress;
try {
validatorAddress = requestContext.getRequiredParameter(0, Address.class);
} catch (JsonRpcParameter.JsonRpcParameterException e) {
throw new InvalidJsonRpcParameters(
"Invalid address parameter", RpcErrorType.INVALID_ADDRESS_PARAMS, e);
}
final Boolean add;
try {
add = requestContext.getRequiredParameter(1, Boolean.class);
} catch (JsonRpcParameter.JsonRpcParameterException e) {
throw new InvalidJsonRpcParameters(
"Invalid vote type parameter", RpcErrorType.INVALID_VOTE_TYPE_PARAMS, e);
}
LOG.trace(
"Received RPC rpcName={} voteType={} address={}",
getName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ public void returnsCorrectMethodName() {
public void exceptionWhenNoParamsSupplied() {
assertThatThrownBy(() -> method.response(requestWithParams()))
.isInstanceOf(InvalidJsonRpcParameters.class)
.hasMessage("Missing required json rpc parameter at index 0");
.hasMessage("Invalid validator address parameter");
}

@Test
public void exceptionWhenInvalidAddressParameterSupplied() {
assertThatThrownBy(() -> method.response(requestWithParams("InvalidAddress")))
.isInstanceOf(InvalidJsonRpcParameters.class)
.hasMessageContaining("Invalid json rpc parameter at index 0");
.hasMessageContaining("Invalid validator address parameter");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ public void returnsCorrectMethodName() {
public void exceptionWhenInvalidStartBlockSupplied() {
assertThatThrownBy(() -> method.response(requestWithParams("INVALID")))
.isInstanceOf(InvalidJsonRpcParameters.class)
.hasMessageContaining("Invalid json rpc parameter at index 0");
.hasMessageContaining("Invalid start block parameter");
}

@Test
public void exceptionWhenInvalidEndBlockSupplied() {
assertThatThrownBy(() -> method.response(requestWithParams("1", "INVALID")))
.isInstanceOf(InvalidJsonRpcParameters.class)
.hasMessageContaining("Invalid json rpc parameter at index 1");
.hasMessageContaining("Invalid end block parameter");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,28 @@ public void returnsCorrectMethodName() {
public void exceptionWhenNoParamsSupplied() {
assertThatThrownBy(() -> method.response(requestWithParams()))
.isInstanceOf(InvalidJsonRpcParameters.class)
.hasMessage("Missing required json rpc parameter at index 0");
.hasMessage("Invalid address parameter");
}

@Test
public void exceptionWhenNoAuthSupplied() {
assertThatThrownBy(() -> method.response(requestWithParams(Address.fromHexString("1"))))
.isInstanceOf(InvalidJsonRpcParameters.class)
.hasMessage("Missing required json rpc parameter at index 1");
.hasMessage("Invalid vote type parameter");
}

@Test
public void exceptionWhenNoAddressSupplied() {
assertThatThrownBy(() -> method.response(requestWithParams("true")))
.isInstanceOf(InvalidJsonRpcParameters.class)
.hasMessageContaining("Invalid json rpc parameter at index 0");
.hasMessageContaining("Invalid address parameter");
}

@Test
public void exceptionWhenInvalidBoolParameterSupplied() {
assertThatThrownBy(() -> method.response(requestWithParams(Address.fromHexString("1"), "c")))
.isInstanceOf(InvalidJsonRpcParameters.class)
.hasMessageContaining("Invalid json rpc parameter at index 1");
.hasMessageContaining("Invalid vote type parameter");
}

@Test
Expand Down
Loading
Loading