From 8348e32e8191f5cb8d470f49d8640167a462d63e Mon Sep 17 00:00:00 2001 From: Daniel Choi <13338103+araskachoi@users.noreply.github.com> Date: Fri, 12 Mar 2021 12:25:35 -0800 Subject: [PATCH] add cmd flag for rpc api modules - stargate (#825) * push changes to cmd for rpcapi options * add flag parse in server file * fix lint --- rpc/apis.go | 91 +++++++++++++++++++++++---------------- rpc/config.go | 9 +++- rpc/namespaces/eth/api.go | 6 ++- server/flags.go | 5 +++ server/start.go | 9 +++- server/util.go | 3 +- 6 files changed, 81 insertions(+), 42 deletions(-) diff --git a/rpc/apis.go b/rpc/apis.go index fc326efd6..e1e7e7907 100644 --- a/rpc/apis.go +++ b/rpc/apis.go @@ -1,16 +1,16 @@ package rpc import ( + "github.com/cosmos/ethermint/rpc/namespaces/eth/filters" + "github.com/cosmos/ethermint/rpc/namespaces/net" + "github.com/cosmos/ethermint/rpc/namespaces/personal" + "github.com/cosmos/ethermint/rpc/namespaces/web3" "github.com/ethereum/go-ethereum/rpc" "github.com/cosmos/ethermint/crypto/ethsecp256k1" "github.com/cosmos/ethermint/rpc/backend" "github.com/cosmos/ethermint/rpc/namespaces/eth" - "github.com/cosmos/ethermint/rpc/namespaces/eth/filters" - "github.com/cosmos/ethermint/rpc/namespaces/net" - "github.com/cosmos/ethermint/rpc/namespaces/personal" - "github.com/cosmos/ethermint/rpc/namespaces/web3" rpctypes "github.com/cosmos/ethermint/rpc/types" "github.com/cosmos/cosmos-sdk/client" @@ -24,44 +24,63 @@ const ( NetNamespace = "net" apiVersion = "1.0" + flagRPCAPI = "rpc-api" ) // GetAPIs returns the list of all APIs from the Ethereum namespaces -func GetAPIs(clientCtx client.Context, keys ...ethsecp256k1.PrivKey) []rpc.API { +func GetAPIs(clientCtx client.Context, selectedApis []string, keys ...ethsecp256k1.PrivKey) []rpc.API { nonceLock := new(rpctypes.AddrLocker) backend := backend.New(clientCtx) ethAPI := eth.NewAPI(clientCtx, backend, nonceLock, keys...) - return []rpc.API{ - { - Namespace: Web3Namespace, - Version: apiVersion, - Service: web3.NewAPI(), - Public: true, - }, - { - Namespace: EthNamespace, - Version: apiVersion, - Service: ethAPI, - Public: true, - }, - { - Namespace: EthNamespace, - Version: apiVersion, - Service: filters.NewAPI(clientCtx, backend), - Public: true, - }, - { - Namespace: PersonalNamespace, - Version: apiVersion, - Service: personal.NewAPI(ethAPI), - Public: false, - }, - { - Namespace: NetNamespace, - Version: apiVersion, - Service: net.NewAPI(clientCtx), - Public: true, - }, + var apis []rpc.API + + for _, api := range selectedApis { + switch api { + case Web3Namespace: + apis = append(apis, + rpc.API{ + Namespace: Web3Namespace, + Version: apiVersion, + Service: web3.NewAPI(), + Public: true, + }, + ) + case EthNamespace: + apis = append(apis, + rpc.API{ + Namespace: EthNamespace, + Version: apiVersion, + Service: ethAPI, + Public: true, + }, + rpc.API{ + Namespace: EthNamespace, + Version: apiVersion, + Service: filters.NewAPI(clientCtx, backend), + Public: true, + }, + ) + case PersonalNamespace: + apis = append(apis, + rpc.API{ + Namespace: PersonalNamespace, + Version: apiVersion, + Service: personal.NewAPI(ethAPI), + Public: false, + }, + ) + case NetNamespace: + apis = append(apis, + rpc.API{ + Namespace: NetNamespace, + Version: apiVersion, + Service: net.NewAPI(clientCtx), + Public: true, + }, + ) + } } + + return apis } diff --git a/rpc/config.go b/rpc/config.go index c487cc58a..b000a5cc6 100644 --- a/rpc/config.go +++ b/rpc/config.go @@ -1,7 +1,10 @@ package rpc import ( + "strings" + "github.com/gorilla/mux" + "github.com/spf13/viper" "github.com/cosmos/cosmos-sdk/client" @@ -14,7 +17,11 @@ func RegisterEthereum(clientCtx client.Context, r *mux.Router) { server := rpc.NewServer() r.HandleFunc("/", server.ServeHTTP).Methods("POST", "OPTIONS") - apis := GetAPIs(clientCtx) + rpcapi := viper.GetString(flagRPCAPI) + rpcapi = strings.ReplaceAll(rpcapi, " ", "") + rpcapiArr := strings.Split(rpcapi, ",") + + apis := GetAPIs(clientCtx, rpcapiArr) // Register all the APIs exposed by the namespace services // TODO: handle allowlist and private APIs diff --git a/rpc/namespaces/eth/api.go b/rpc/namespaces/eth/api.go index 76e86b948..ae1df93e9 100644 --- a/rpc/namespaces/eth/api.go +++ b/rpc/namespaces/eth/api.go @@ -19,7 +19,6 @@ import ( "github.com/cosmos/ethermint/rpc/backend" rpctypes "github.com/cosmos/ethermint/rpc/types" ethermint "github.com/cosmos/ethermint/types" - "github.com/cosmos/ethermint/x/evm/types" evmtypes "github.com/cosmos/ethermint/x/evm/types" abci "github.com/tendermint/tendermint/abci/types" @@ -220,6 +219,7 @@ func (api *PublicEthereumAPI) BlockNumber() (hexutil.Uint64, error) { } // GetBalance returns the provided account's balance up to the provided block number. +//nolint:interfacer func (api *PublicEthereumAPI) GetBalance(address common.Address, blockNum rpctypes.BlockNumber) (*hexutil.Big, error) { api.logger.Debug("eth_getBalance", "address", address, "block number", blockNum) @@ -271,6 +271,7 @@ func (api *PublicEthereumAPI) GetBalance(address common.Address, blockNum rpctyp } // GetStorageAt returns the contract storage at the given address, block number, and key. +//nolint:interfacer func (api *PublicEthereumAPI) GetStorageAt(address common.Address, key string, blockNum rpctypes.BlockNumber) (hexutil.Bytes, error) { api.logger.Debug("eth_getStorageAt", "address", address, "key", key, "block number", blockNum) @@ -375,6 +376,7 @@ func (api *PublicEthereumAPI) GetUncleCountByBlockNumber(_ rpctypes.BlockNumber) } // GetCode returns the contract code at the given address and block number. +//nolint:interfacer func (api *PublicEthereumAPI) GetCode(address common.Address, blockNumber rpctypes.BlockNumber) (hexutil.Bytes, error) { api.logger.Debug("eth_getCode", "address", address, "block number", blockNumber) @@ -614,7 +616,7 @@ func (api *PublicEthereumAPI) doCall( // NOTE: we query the EVM denomination to allow other chains to use their custom denomination as // the fee token - paramsRes, err := api.queryClient.Params(api.ctx, &types.QueryParamsRequest{}) + paramsRes, err := api.queryClient.Params(api.ctx, &evmtypes.QueryParamsRequest{}) if err != nil { return nil, err } diff --git a/server/flags.go b/server/flags.go index fd51daca4..b17691bfa 100644 --- a/server/flags.go +++ b/server/flags.go @@ -15,6 +15,11 @@ const ( flagGRPCAddress = "grpc.address" ) +// RPCAPI-related flags. +const ( + flagRPCAPI = "rpc-api" +) + // Ethereum-related flags. const ( flagJSONRPCEnable = "json-rpc.enable" diff --git a/server/start.go b/server/start.go index 5a7925c6a..9055c5358 100644 --- a/server/start.go +++ b/server/start.go @@ -6,6 +6,7 @@ import ( "fmt" "os" "runtime/pprof" + "strings" "time" "github.com/tendermint/tendermint/rpc/client/local" @@ -125,6 +126,8 @@ which accepts a path for the resulting pprof file. cmd.Flags().String(flags.FlagKeyringBackend, flags.DefaultKeyringBackend, "Select keyring's backend (os|file|kwallet|pass|test)") + cmd.Flags().String(flagRPCAPI, "", fmt.Sprintf("Comma separated list of RPC API modules to enable: %s, %s, %s, %s", rpc.Web3Namespace, rpc.EthNamespace, rpc.PersonalNamespace, rpc.NetNamespace)) + // add support for all Tendermint-specific command line options tcmd.AddNodeFlags(cmd) return cmd @@ -276,7 +279,11 @@ func startInProcess(ctx *sdkserver.Context, clientCtx client.Context, appCreator } } - jsonRPCSrv := jsonrpc.NewService(rpc.GetAPIs(clientCtx)) + rpcapi := ctx.Viper.GetString(flagRPCAPI) + rpcapi = strings.ReplaceAll(rpcapi, " ", "") + rpcapiArr := strings.Split(rpcapi, ",") + + jsonRPCSrv := jsonrpc.NewService(rpc.GetAPIs(clientCtx, rpcapiArr)) if err := jsonRPCSrv.RegisterRoutes(); err != nil { return err diff --git a/server/util.go b/server/util.go index c17af38ca..ea650eb99 100644 --- a/server/util.go +++ b/server/util.go @@ -18,7 +18,6 @@ import ( "github.com/cosmos/cosmos-sdk/client/flags" sdkserver "github.com/cosmos/cosmos-sdk/server" - "github.com/cosmos/cosmos-sdk/server/types" servertypes "github.com/cosmos/cosmos-sdk/server/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/version" @@ -155,7 +154,7 @@ func interceptConfigs(rootViper *viper.Viper) (*tmcfg.Config, error) { // AddCommands adds the server commands func AddCommands( rootCmd *cobra.Command, defaultNodeHome string, - appCreator servertypes.AppCreator, appExport types.AppExporter, addStartFlags types.ModuleInitFlags, + appCreator servertypes.AppCreator, appExport servertypes.AppExporter, addStartFlags servertypes.ModuleInitFlags, ) { tendermintCmd := &cobra.Command{ Use: "tendermint",