Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

add cmd flag for rpc api modules - stargate #825

Merged
merged 4 commits into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 55 additions & 36 deletions rpc/apis.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
}
9 changes: 8 additions & 1 deletion rpc/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package rpc

import (
"strings"

"github.com/gorilla/mux"
"github.com/spf13/viper"

"github.com/cosmos/cosmos-sdk/client"

Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions rpc/namespaces/eth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 5 additions & 0 deletions server/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ const (
flagGRPCAddress = "grpc.address"
)

// RPCAPI-related flags.
const (
flagRPCAPI = "rpc-api"
)

// Ethereum-related flags.
const (
flagJSONRPCEnable = "json-rpc.enable"
Expand Down
9 changes: 8 additions & 1 deletion server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"runtime/pprof"
"strings"
"time"

"github.com/tendermint/tendermint/rpc/client/local"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand Down