Skip to content

Commit

Permalink
5098 branch 17 update extra data and filter params (hyperledger#7458)
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 15, 2024
1 parent 137515d commit f5d6e72
Show file tree
Hide file tree
Showing 27 changed files with 146 additions and 46 deletions.
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.filter.FilterManager;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
Expand All @@ -43,7 +44,13 @@ public String getName() {

@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
final String filterId = requestContext.getRequiredParameter(0, String.class);
final String filterId;
try {
filterId = requestContext.getRequiredParameter(0, String.class);
} catch (Exception e) { // TODO:replace with JsonRpcParameter.JsonRpcParameterException
throw new InvalidJsonRpcParameters(
"Invalid filter ID parameter (index 0)", RpcErrorType.INVALID_FILTER_PARAMS, e);
}

final List<Hash> blockHashes = filterManager.blockChanges(filterId);
if (blockHashes != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

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.filter.FilterManager;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
Expand All @@ -41,7 +42,13 @@ public String getName() {

@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
final String filterId = requestContext.getRequiredParameter(0, String.class);
final String filterId;
try {
filterId = requestContext.getRequiredParameter(0, String.class);
} catch (Exception e) { // TODO:replace with JsonRpcParameter.JsonRpcParameterException
throw new InvalidJsonRpcParameters(
"Invalid filter ID parameter (index 0)", RpcErrorType.INVALID_FILTER_PARAMS, e);
}

final List<LogWithMetadata> logs = filterManager.logs(filterId);
if (logs != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,18 @@ public String getName() {

@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
final FilterParameter filter = requestContext.getRequiredParameter(0, FilterParameter.class);
final FilterParameter filter;
try {
filter = requestContext.getRequiredParameter(0, FilterParameter.class);
} catch (Exception e) {
throw new InvalidJsonRpcParameters(
"Invalid filter parameter (index 0)", RpcErrorType.INVALID_FILTER_PARAMS, e);
}
LOG.atTrace().setMessage("eth_getLogs FilterParameter: {}").addArgument(filter).log();

if (!filter.isValid()) {
return new JsonRpcErrorResponse(
requestContext.getRequest().getId(), RpcErrorType.INVALID_PARAMS);
requestContext.getRequest().getId(), RpcErrorType.INVALID_FILTER_PARAMS);
}

final AtomicReference<Exception> ex = new AtomicReference<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

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.filter.FilterManager;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.FilterParameter;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
Expand All @@ -38,11 +39,17 @@ public String getName() {

@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
final FilterParameter filter = requestContext.getRequiredParameter(0, FilterParameter.class);
final FilterParameter filter;
try {
filter = requestContext.getRequiredParameter(0, FilterParameter.class);
} catch (Exception e) { // TODO:replace with JsonRpcParameter.JsonRpcParameterException
throw new InvalidJsonRpcParameters(
"Invalid filter paramters (index 0)", RpcErrorType.INVALID_FILTER_PARAMS, e);
}

if (!filter.isValid()) {
return new JsonRpcErrorResponse(
requestContext.getRequest().getId(), RpcErrorType.INVALID_PARAMS);
requestContext.getRequest().getId(), RpcErrorType.INVALID_FILTER_PARAMS);
}

final String logFilterId =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

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.filter.FilterManager;
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;

public class EthUninstallFilter implements JsonRpcMethod {

Expand All @@ -35,7 +37,13 @@ public String getName() {

@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
final String filterId = requestContext.getRequiredParameter(0, String.class);
final String filterId;
try {
filterId = requestContext.getRequiredParameter(0, String.class);
} catch (Exception e) { // TODO:replace with JsonRpcParameter.JsonRpcParameterException
throw new InvalidJsonRpcParameters(
"Invalid filter ID parameter (index 0)", RpcErrorType.INVALID_FILTER_PARAMS, e);
}

return new JsonRpcSuccessResponse(
requestContext.getRequest().getId(), filterManager.uninstallFilter(filterId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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.parameters.BlockParameter;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.FilterParameter;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.BlockTracer;
Expand Down Expand Up @@ -86,8 +87,13 @@ public String getName() {

@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
final FilterParameter filterParameter =
requestContext.getRequiredParameter(0, FilterParameter.class);
final FilterParameter filterParameter;
try {
filterParameter = requestContext.getRequiredParameter(0, FilterParameter.class);
} catch (Exception e) { // TODO:replace with JsonRpcParameter.JsonRpcParameterException
throw new InvalidJsonRpcParameters(
"Invalid filter parameter (index 0)", RpcErrorType.INVALID_FILTER_PARAMS, e);
}

final long fromBlock = resolveBlockNumber(filterParameter.getFromBlock());
final long toBlock = resolveBlockNumber(filterParameter.getToBlock());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
.addArgument(() -> new String(extraData.toArray(), StandardCharsets.UTF_8))
.log();
return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), true);
} catch (final IllegalArgumentException invalidJsonRpcParameters) {
} catch (Exception invalidJsonRpcParameters) { // TODO:replace with "IllegalArgumentException |
// JsonRpcParameter.JsonRpcParameterException"
return new JsonRpcErrorResponse(
requestContext.getRequest().getId(),
new JsonRpcError(RpcErrorType.INVALID_PARAMS, invalidJsonRpcParameters.getMessage()));
new JsonRpcError(
RpcErrorType.INVALID_EXTRA_DATA_PARAMS, invalidJsonRpcParameters.getMessage()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.transaction.pool.Predicate.EQ;

import org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcParameters;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.transaction.pool.PendingTransactionFilter.Filter;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.transaction.pool.Predicate;

Expand Down Expand Up @@ -79,25 +80,31 @@ public List<Filter> filters() throws IllegalArgumentException {
private Optional<Filter> getFilter(final String key, final Map<String, String> map) {
if (map != null) {
if (map.size() > 1) {
throw new InvalidJsonRpcParameters("Only one operator per filter type allowed");
throw new InvalidJsonRpcParameters(
"Only one operator per filter type allowed", RpcErrorType.INVALID_FILTER_PARAMS);
} else if (!map.isEmpty()) {
final Map.Entry<String, String> foundEntry = map.entrySet().stream().findFirst().get();
final Predicate predicate =
Predicate.fromValue(foundEntry.getKey().toUpperCase(Locale.ROOT))
.orElseThrow(
() ->
new InvalidJsonRpcParameters(
"Unknown field expected one of `eq`, `gt`, `lt`, `action`"));
"Unknown field expected one of `eq`, `gt`, `lt`, `action`",
RpcErrorType.INVALID_FILTER_PARAMS));

final Filter filter = new Filter(key, foundEntry.getValue(), predicate);
if (key.equals(FROM_FIELD) && !predicate.equals(EQ)) {
throw new InvalidJsonRpcParameters("The `from` filter only supports the `eq` operator");
throw new InvalidJsonRpcParameters(
"The `from` filter only supports the `eq` operator",
RpcErrorType.INVALID_FILTER_PARAMS);
} else if (key.equals(TO_FIELD) && !predicate.equals(EQ) && !predicate.equals(ACTION)) {
throw new InvalidJsonRpcParameters(
"The `to` filter only supports the `eq` or `action` operator");
"The `to` filter only supports the `eq` or `action` operator",
RpcErrorType.INVALID_FILTER_PARAMS);
} else if (!key.equals(TO_FIELD) && predicate.equals(ACTION)) {
throw new InvalidJsonRpcParameters(
"The operator `action` is only supported by the `to` filter");
"The operator `action` is only supported by the `to` filter",
RpcErrorType.INVALID_FILTER_PARAMS);
}
return Optional.of(filter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

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.filter.FilterManager;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
Expand Down Expand Up @@ -52,7 +53,13 @@ public String getName() {
@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
final String privacyGroupId = requestContext.getRequiredParameter(0, String.class);
final String filterId = requestContext.getRequiredParameter(1, String.class);
final String filterId;
try {
filterId = requestContext.getRequiredParameter(1, String.class);
} catch (Exception e) { // TODO:replace with JsonRpcParameter.JsonRpcParameterException
throw new InvalidJsonRpcParameters(
"Invalid filter ID parameter (index 1)", RpcErrorType.INVALID_FILTER_PARAMS, e);
}

if (privacyController instanceof MultiTenancyPrivacyController) {
checkIfPrivacyGroupMatchesAuthenticatedPrivacyUserId(requestContext, privacyGroupId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

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.filter.FilterManager;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
Expand Down Expand Up @@ -52,7 +53,13 @@ public String getName() {
@Override
public JsonRpcResponse response(final JsonRpcRequestContext request) {
final String privacyGroupId = request.getRequiredParameter(0, String.class);
final String filterId = request.getRequiredParameter(1, String.class);
final String filterId;
try {
filterId = request.getRequiredParameter(1, String.class);
} catch (Exception e) { // TODO:replace with JsonRpcParameter.JsonRpcParameterException
throw new InvalidJsonRpcParameters(
"Invalid filter ID parameter (index 1)", RpcErrorType.INVALID_FILTER_PARAMS, e);
}

if (privacyController instanceof MultiTenancyPrivacyController) {
checkIfPrivacyGroupMatchesAuthenticatedPrivacyUserId(request, privacyGroupId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@

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.filter.FilterManager;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
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.privacy.MultiTenancyPrivacyController;
import org.hyperledger.besu.ethereum.privacy.PrivacyController;

Expand All @@ -46,7 +48,13 @@ public String getName() {
@Override
public JsonRpcResponse response(final JsonRpcRequestContext request) {
final String privacyGroupId = request.getRequiredParameter(0, String.class);
final String filterId = request.getRequiredParameter(1, String.class);
final String filterId;
try {
filterId = request.getRequiredParameter(1, String.class);
} catch (Exception e) {
throw new InvalidJsonRpcParameters(
"Invalid filter ID paramter (index 1)", RpcErrorType.INVALID_FILTER_PARAMS, e);
}

if (privacyController instanceof MultiTenancyPrivacyController) {
checkIfPrivacyGroupMatchesAuthenticatedEnclaveKey(request, privacyGroupId);
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.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.FilterParameter;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.PrivacyIdProvider;
Expand Down Expand Up @@ -62,11 +63,17 @@ public String getName() {
@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
final String privacyGroupId = requestContext.getRequiredParameter(0, String.class);
final FilterParameter filter = requestContext.getRequiredParameter(1, FilterParameter.class);
final FilterParameter filter;
try {
filter = requestContext.getRequiredParameter(1, FilterParameter.class);
} catch (Exception e) { // TODO:replace with JsonRpcParameter.JsonRpcParameterException
throw new InvalidJsonRpcParameters(
"Invalid filter parameter (index 1)", RpcErrorType.INVALID_FILTER_PARAMS, e);
}

if (!filter.isValid()) {
return new JsonRpcErrorResponse(
requestContext.getRequest().getId(), RpcErrorType.INVALID_PARAMS);
requestContext.getRequest().getId(), RpcErrorType.INVALID_FILTER_PARAMS);
}

final List<LogWithMetadata> matchingLogs =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

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.filter.FilterManager;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.FilterParameter;
Expand Down Expand Up @@ -50,7 +51,13 @@ public String getName() {
@Override
public JsonRpcResponse response(final JsonRpcRequestContext request) {
final String privacyGroupId = request.getRequiredParameter(0, String.class);
final FilterParameter filter = request.getRequiredParameter(1, FilterParameter.class);
final FilterParameter filter;
try {
filter = request.getRequiredParameter(1, FilterParameter.class);
} catch (Exception e) { // TODO:replace with JsonRpcParameter.JsonRpcParameterException
throw new InvalidJsonRpcParameters(
"Invalid filter parameter (index 1)", RpcErrorType.INVALID_FILTER_PARAMS, e);
}
final String privacyUserId = privacyIdProvider.getPrivacyUserId(request.getUser());

if (privacyController instanceof MultiTenancyPrivacyController) {
Expand All @@ -60,7 +67,8 @@ public JsonRpcResponse response(final JsonRpcRequestContext request) {
}

if (!filter.isValid()) {
return new JsonRpcErrorResponse(request.getRequest().getId(), RpcErrorType.INVALID_PARAMS);
return new JsonRpcErrorResponse(
request.getRequest().getId(), RpcErrorType.INVALID_FILTER_PARAMS);
}

final String logFilterId =
Expand Down
Loading

0 comments on commit f5d6e72

Please sign in to comment.