Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Commit

Permalink
added error message for RPC method disabled (#1396)
Browse files Browse the repository at this point in the history
* added error message for RPC method disabled

* added methods to enum

* added clique and ibft methods
  • Loading branch information
macfarla authored May 7, 2019
1 parent ae7d77a commit 6451d28
Show file tree
Hide file tree
Showing 87 changed files with 340 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import tech.pegasys.pantheon.consensus.common.VoteTallyCache;
import tech.pegasys.pantheon.ethereum.core.BlockHeader;
import tech.pegasys.pantheon.ethereum.jsonrpc.RpcMethod;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.JsonRpcMethod;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.parameters.BlockParameter;
Expand All @@ -30,7 +31,6 @@
import java.util.stream.Collectors;

public class CliqueGetSigners implements JsonRpcMethod {
public static final String CLIQUE_GET_SIGNERS = "clique_getSigners";
private final BlockchainQueries blockchainQueries;
private final VoteTallyCache voteTallyCache;
private final JsonRpcParameter parameters;
Expand All @@ -46,7 +46,7 @@ public CliqueGetSigners(

@Override
public String getName() {
return CLIQUE_GET_SIGNERS;
return RpcMethod.CLIQUE_GET_SIGNERS.getMethodName();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import tech.pegasys.pantheon.consensus.common.VoteTallyCache;
import tech.pegasys.pantheon.ethereum.core.BlockHeader;
import tech.pegasys.pantheon.ethereum.core.Hash;
import tech.pegasys.pantheon.ethereum.jsonrpc.RpcMethod;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.JsonRpcMethod;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.parameters.JsonRpcParameter;
Expand All @@ -30,7 +31,6 @@
import java.util.stream.Collectors;

public class CliqueGetSignersAtHash implements JsonRpcMethod {
public static final String CLIQUE_GET_SIGNERS_AT_HASH = "clique_getSignersAtHash";
private final BlockchainQueries blockchainQueries;
private final VoteTallyCache voteTallyCache;
private final JsonRpcParameter parameters;
Expand All @@ -46,7 +46,7 @@ public CliqueGetSignersAtHash(

@Override
public String getName() {
return CLIQUE_GET_SIGNERS_AT_HASH;
return RpcMethod.CLIQUE_GET_SIGNERS_AT_HASH.getMethodName();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import tech.pegasys.pantheon.consensus.common.VoteProposer;
import tech.pegasys.pantheon.consensus.common.jsonrpc.AbstractVoteProposerMethod;
import tech.pegasys.pantheon.ethereum.jsonrpc.RpcMethod;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.JsonRpcMethod;

public class CliqueProposals extends AbstractVoteProposerMethod implements JsonRpcMethod {
Expand All @@ -24,6 +25,6 @@ public CliqueProposals(final VoteProposer voteProposer) {

@Override
public String getName() {
return "clique_proposals";
return RpcMethod.CLIQUE_GET_PROPOSALS.getMethodName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@

import tech.pegasys.pantheon.consensus.common.VoteProposer;
import tech.pegasys.pantheon.ethereum.core.Address;
import tech.pegasys.pantheon.ethereum.jsonrpc.RpcMethod;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.JsonRpcMethod;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.parameters.JsonRpcParameter;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcResponse;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse;

public class Discard implements JsonRpcMethod {
private static final String CLIQUE_DISCARD = "clique_discard";
private final VoteProposer proposer;
private final JsonRpcParameter parameters;

Expand All @@ -32,7 +32,7 @@ public Discard(final VoteProposer proposer, final JsonRpcParameter parameters) {

@Override
public String getName() {
return CLIQUE_DISCARD;
return RpcMethod.CLIQUE_DISCARD.getMethodName();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import tech.pegasys.pantheon.consensus.common.VoteProposer;
import tech.pegasys.pantheon.ethereum.core.Address;
import tech.pegasys.pantheon.ethereum.jsonrpc.RpcMethod;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.JsonRpcMethod;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.parameters.JsonRpcParameter;
Expand All @@ -35,7 +36,7 @@ public Propose(final VoteProposer proposer, final JsonRpcParameter parameters) {

@Override
public String getName() {
return "clique_propose";
return RpcMethod.CLIQUE_PROPOSE.getMethodName();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import tech.pegasys.pantheon.consensus.common.VoteProposer;
import tech.pegasys.pantheon.ethereum.core.Address;
import tech.pegasys.pantheon.ethereum.jsonrpc.RpcMethod;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.JsonRpcMethod;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.parameters.JsonRpcParameter;
Expand All @@ -36,7 +37,7 @@ public IbftDiscardValidatorVote(

@Override
public String getName() {
return "ibft_discardValidatorVote";
return RpcMethod.IBFT_DISCARD_VALIDATOR_VOTE.getMethodName();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import tech.pegasys.pantheon.consensus.common.VoteProposer;
import tech.pegasys.pantheon.consensus.common.jsonrpc.AbstractVoteProposerMethod;
import tech.pegasys.pantheon.ethereum.jsonrpc.RpcMethod;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.JsonRpcMethod;

public class IbftGetPendingVotes extends AbstractVoteProposerMethod implements JsonRpcMethod {
Expand All @@ -24,6 +25,6 @@ public IbftGetPendingVotes(final VoteProposer voteProposer) {

@Override
public String getName() {
return "ibft_getPendingVotes";
return RpcMethod.IBFT_GET_PENDING_VOTES.getMethodName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import tech.pegasys.pantheon.ethereum.chain.Blockchain;
import tech.pegasys.pantheon.ethereum.core.BlockHeader;
import tech.pegasys.pantheon.ethereum.core.Hash;
import tech.pegasys.pantheon.ethereum.jsonrpc.RpcMethod;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.JsonRpcMethod;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.parameters.JsonRpcParameter;
Expand Down Expand Up @@ -46,7 +47,7 @@ public IbftGetValidatorsByBlockHash(

@Override
public String getName() {
return "ibft_getValidatorsByBlockHash";
return RpcMethod.IBFT_GET_VALIDATORS_BY_BLOCK_HASH.getMethodName();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import tech.pegasys.pantheon.consensus.common.BlockInterface;
import tech.pegasys.pantheon.ethereum.core.BlockHeader;
import tech.pegasys.pantheon.ethereum.jsonrpc.RpcMethod;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.AbstractBlockParameterMethod;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.JsonRpcMethod;
Expand Down Expand Up @@ -62,6 +63,6 @@ protected Object resultByBlockNumber(final JsonRpcRequest request, final long bl

@Override
public String getName() {
return "ibft_getValidatorsByBlockNumber";
return RpcMethod.IBFT_GET_VALIDATORS_BY_BLOCK_NUMBER.getMethodName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import tech.pegasys.pantheon.consensus.common.VoteProposer;
import tech.pegasys.pantheon.consensus.common.VoteType;
import tech.pegasys.pantheon.ethereum.core.Address;
import tech.pegasys.pantheon.ethereum.jsonrpc.RpcMethod;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.JsonRpcMethod;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.parameters.JsonRpcParameter;
Expand All @@ -37,7 +38,7 @@ public IbftProposeValidatorVote(

@Override
public String getName() {
return "ibft_proposeValidatorVote";
return RpcMethod.IBFT_PROPOSE_VALIDATOR_VOTE.getMethodName();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public class JsonRpcHttpService {

private final Vertx vertx;
private final JsonRpcConfiguration config;
private final Map<String, JsonRpcMethod> jsonRpcMethods;
private final RpcMethods rpcMethods;
private final Path dataDir;
private final LabelledMetric<OperationTimer> requestTimer;

Expand Down Expand Up @@ -129,7 +129,7 @@ private JsonRpcHttpService(
validateConfig(config);
this.config = config;
this.vertx = vertx;
this.jsonRpcMethods = methods;
this.rpcMethods = new RpcMethods(methods);
this.authenticationService = authenticationService;
}

Expand Down Expand Up @@ -442,9 +442,14 @@ private JsonRpcResponse process(final JsonObject requestJson, final Optional<Use

LOG.debug("JSON-RPC request -> {}", request.getMethod());
// Find method handler
final JsonRpcMethod method = jsonRpcMethods.get(request.getMethod());
final JsonRpcMethod method = rpcMethods.getMethod(request.getMethod());
if (method == null) {
return errorResponse(id, JsonRpcError.METHOD_NOT_FOUND);
if (!rpcMethods.isDefined(request.getMethod())) {
return errorResponse(id, JsonRpcError.METHOD_NOT_FOUND);
}
if (!rpcMethods.isEnabled(request.getMethod())) {
return errorResponse(id, JsonRpcError.METHOD_NOT_ENABLED);
}
}

if (AuthenticationUtils.isPermitted(authenticationService, user, method)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Copyright 2019 ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.pantheon.ethereum.jsonrpc;

import java.util.ArrayList;
import java.util.Collection;

public enum RpcMethod {
ADMIN_ADD_PEER("admin_addPeer"),
ADMIN_NODE_INFO("admin_nodeInfo"),
ADMIN_PEERS("admin_peers"),
ADMIN_REMOVE_PEER("admin_removePeer"),
CLIQUE_DISCARD("clique_discard"),
CLIQUE_GET_SIGNERS("clique_getSigners"),
CLIQUE_GET_SIGNERS_AT_HASH("clique_getSignersAtHash"),
CLIQUE_GET_PROPOSALS("clique_proposals"),
CLIQUE_PROPOSE("clique_propose"),
DEBUG_METRICS("debug_metrics"),
DEBUG_STORAGE_RANGE_AT("debug_storageRangeAt"),
DEBUG_TRACE_BLOCK("debug_traceBlock"),
DEBUG_TRACE_BLOCK_BY_HASH("debug_traceBlockByHash"),
DEBUG_TRACE_BLOCK_BY_NUMBER("debug_traceBlockByNumber"),
DEBUG_TRACE_TRANSACTION("debug_traceTransaction"),
EEA_GET_TRANSACTION_COUNT("eea_getTransactionCount"),
EEA_GET_TRANSACTION_RECEIPT("eea_getTransactionReceipt"),
EEA_SEND_RAW_TRANSACTION("eea_sendRawTransaction"),
ETH_ACCOUNTS("eth_accounts"),
ETH_BLOCK_NUMBER("eth_blockNumber"),
ETH_CALL("eth_call"),
ETH_CHAIN_ID("eth_chainId"),
ETH_COINBASE("eth_coinbase"),
ETH_ESTIMATE_GAS("eth_estimateGas"),
ETH_GAS_PRICE("eth_gasPrice"),
ETH_GET_BALANCE("eth_getBalance"),
ETH_GET_BLOCK_BY_HASH("eth_getBlockByHash"),
ETH_GET_BLOCK_BY_NUMBER("eth_getBlockByNumber"),
ETH_GET_BLOCK_TRANSACTION_COUNT_BY_HASH("eth_getBlockTransactionCountByHash"),
ETH_GET_BLOCK_TRANSACTION_COUNT_BY_NUMBER("eth_getBlockTransactionCountByNumber"),
ETH_GET_CODE("eth_getCode"),
ETH_GET_FILTER_CHANGES("eth_getFilterChanges"),
ETH_GET_FILTER_LOGS("eth_getFilterLogs"),
ETH_GET_LOGS("eth_getLogs"),
ETH_GET_STORAGE_AT("eth_getStorageAt"),
ETH_GET_TRANSACTION_BY_BLOCK_HASH_AND_INDEX("eth_getTransactionByBlockHashAndIndex"),
ETH_GET_TRANSACTION_BY_BLOCK_NUMBER_AND_INDEX("eth_getTransactionByBlockNumberAndIndex"),
ETH_GET_TRANSACTION_BY_HASH("eth_getTransactionByHash"),
ETH_GET_TRANSACTION_COUNT("eth_getTransactionCount"),
ETH_GET_TRANSACTION_RECEIPT("eth_getTransactionReceipt"),
ETH_GET_UNCLE_BY_BLOCK_HASH_AND_INDEX("eth_getUncleByBlockHashAndIndex"),
ETH_GET_UNCLE_BY_BLOCK_NUMBER_AND_INDEX("eth_getUncleByBlockNumberAndIndex"),
ETH_GET_UNCLE_COUNT_BY_BLOCK_HASH("eth_getUncleCountByBlockHash"),
ETH_GET_UNCLE_COUNT_BY_BLOCK_NUMBER("eth_getUncleCountByBlockNumber"),
ETH_GET_WORK("eth_getWork"),
ETH_HASHRATE("eth_hashrate"),
ETH_MINING("eth_mining"),
ETH_NEW_BLOCK_FILTER("eth_newBlockFilter"),
ETH_NEW_FILTER("eth_newFilter"),
ETH_NEW_PENDING_TRANSACTION_FILTER("eth_newPendingTransactionFilter"),
ETH_PROTOCOL_VERSION("eth_protocolVersion"),
ETH_SEND_RAW_TRANSACTION("eth_sendRawTransaction"),
ETH_SUBSCRIBE("eth_subscribe"),
ETH_SYNCING("eth_syncing"),
ETH_UNINSTALL_FILTER("eth_uninstallFilter"),
ETH_UNSUBSCRIBE("eth_unsubscribe"),
IBFT_DISCARD_VALIDATOR_VOTE("ibft_discardValidatorVote"),
IBFT_GET_PENDING_VOTES("ibft_getPendingVotes"),
IBFT_GET_VALIDATORS_BY_BLOCK_HASH("ibft_getValidatorsByBlockHash"),
IBFT_GET_VALIDATORS_BY_BLOCK_NUMBER("ibft_getValidatorsByBlockNumber"),
IBFT_PROPOSE_VALIDATOR_VOTE("ibft_proposeValidatorVote"),
MINER_SET_COINBASE("miner_setCoinbase"),
MINER_SET_ETHERBASE("miner_setEtherbase"),
MINER_START("miner_start"),
MINER_STOP("miner_stop"),
NET_ENODE("net_enode"),
NET_LISTENING("net_listening"),
NET_PEER_COUNT("net_peerCount"),
NET_SERVICES("net_services"),
NET_VERSION("net_version"),
PERM_ADD_ACCOUNTS_TO_WHITELIST("perm_addAccountsToWhitelist"),
PERM_ADD_NODES_TO_WHITELIST("perm_addNodesToWhitelist"),
PERM_GET_ACCOUNTS_WHITELIST("perm_getAccountsWhitelist"),
PERM_GET_NODES_WHITELIST("perm_getNodesWhitelist"),
PERM_RELOAD_PERMISSIONS_FROM_FILE("perm_reloadPermissionsFromFile"),
PERM_REMOVE_ACCOUNTS_FROM_WHITELIST("perm_removeAccountsFromWhitelist"),
PERM_REMOVE_NODES_FROM_WHITELIST("perm_removeNodesFromWhitelist"),
RPC_MODULES("rpc_modules"),
TX_POOL_PANTHEON_STATISTICS("txpool_pantheonStatistics"),
TX_POOL_PANTHEON_TRANSACTIONS("txpool_pantheonTransactions"),
WEB3_CLIENT_VERSION("web3_clientVersion"),
WEB3_SHA3("web3_sha3");

private final String methodName;

private static Collection<String> allMethodNames;

public String getMethodName() {
return methodName;
}

static {
allMethodNames = new ArrayList<>();
for (RpcMethod m : RpcMethod.values()) {
allMethodNames.add(m.getMethodName());
}
}

RpcMethod(final String methodName) {
this.methodName = methodName;
}

public static boolean rpcMethodExists(final String rpcMethodName) {
return allMethodNames.contains(rpcMethodName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2019 ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.pantheon.ethereum.jsonrpc;

import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.JsonRpcMethod;

import java.util.Map;

public class RpcMethods {

private Map<String, JsonRpcMethod> enabledMethods;

public RpcMethods(final Map<String, JsonRpcMethod> enabledMethods) {
this.enabledMethods = enabledMethods;
}

public JsonRpcMethod getMethod(final String methodName) {
return enabledMethods.get(methodName);
}

public boolean isEnabled(final String methodName) {
return enabledMethods.containsKey(methodName);
}

public boolean isDefined(final String methodName) {
return RpcMethod.rpcMethodExists(methodName) || isEnabled(methodName);
}
}
Loading

0 comments on commit 6451d28

Please sign in to comment.