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",