Enable RPC Server (#75)

- Introduces rpc command to cli (rest-server)
- Moves server/rpc to rpc/
- Enables module selection (eg. "web3" or "eth" or "web3,eth"), however there is no CLI flag to configure these (See #74)
- Adds CLI context to eth API
This commit is contained in:
David Ansermino 2019-07-15 10:13:59 -04:00 committed by GitHub
parent fbaa9466b0
commit 284c2a0333
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 100 additions and 46 deletions

View File

@ -6,10 +6,11 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/client/rpc"
sdkrpc "github.com/cosmos/cosmos-sdk/client/rpc"
sdk "github.com/cosmos/cosmos-sdk/types"
emintapp "github.com/cosmos/ethermint/app"
"github.com/cosmos/ethermint/rpc"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tendermint/tendermint/libs/cli"
@ -18,7 +19,7 @@ import (
func main() {
cobra.EnableCommandSorting = false
// TODO: Set up codec
cdc := emintapp.MakeCodec()
// Read in the configuration file for the sdk
config := sdk.GetConfig()
@ -40,12 +41,12 @@ func main() {
// Construct Root Command
rootCmd.AddCommand(
rpc.StatusCommand(),
sdkrpc.StatusCommand(),
client.ConfigCmd(emintapp.DefaultCLIHome),
// TODO: Set up query command
// TODO: Set up tx command
// TODO: Set up rest routes (if included, different from web3 api)
// TODO: Set up web3 API setup command?
rpc.Web3RpcCmd(cdc),
client.LineBreak,
keys.Commands(),
client.LineBreak,

24
rpc/apis.go Normal file
View File

@ -0,0 +1,24 @@
// Package rpc contains RPC handler methods and utilities to start
// Ethermint's Web3-compatibly JSON-RPC server.
package rpc
import (
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/ethereum/go-ethereum/rpc"
)
// GetRPCAPIs returns the list of all APIs
func GetRPCAPIs(cliCtx context.CLIContext) []rpc.API {
return []rpc.API{
{
Namespace: "web3",
Version: "1.0",
Service: NewPublicWeb3API(),
},
{
Namespace: "eth",
Version: "1.0",
Service: NewPublicEthAPI(cliCtx),
},
}
}

View File

@ -2,6 +2,7 @@ package rpc
import (
"context"
sdkcontext"github.com/cosmos/cosmos-sdk/client/context"
"testing"
"time"
@ -67,7 +68,7 @@ func startAPIServer() (context.CancelFunc, int, error) {
ctx, cancel := context.WithCancel(context.Background())
_, err := StartHTTPEndpoint(ctx, config, GetRPCAPIs(), timeouts)
_, err := StartHTTPEndpoint(ctx, config, GetRPCAPIs(sdkcontext.NewCLIContext()), timeouts)
if err != nil {
return cancel, 0, err
}

60
rpc/config.go Normal file
View File

@ -0,0 +1,60 @@
package rpc
import (
"github.com/cosmos/cosmos-sdk/client/lcd"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/ethereum/go-ethereum/rpc"
"github.com/spf13/cobra"
"log"
)
// defaultModules returns all available modules
func defaultModules() []string {
return []string{"web3", "eth"}
}
// Config contains configuration fields that determine the behavior of the RPC HTTP server.
// TODO: These may become irrelevant if HTTP config is handled by the SDK
type Config struct {
// EnableRPC defines whether or not to enable the RPC server
EnableRPC bool
// RPCAddr defines the IP address to listen on
RPCAddr string
// RPCPort defines the port to listen on
RPCPort int
// RPCCORSDomains defines list of domains to enable CORS headers for (used by browsers)
RPCCORSDomains []string
// RPCVhosts defines list of domains to listen on (useful if Tendermint is addressable via DNS)
RPCVHosts []string
}
// Web3RpcCmd creates a CLI command to start RPC server
func Web3RpcCmd(cdc *codec.Codec) *cobra.Command {
return lcd.ServeCommand(cdc, registerRoutes)
}
// registerRoutes creates a new server and registers the `/rpc` endpoint.
// Rpc calls are enabled based on their associated module (eg. "eth").
func registerRoutes(rs *lcd.RestServer) {
s := rpc.NewServer()
apis := GetRPCAPIs(rs.CliCtx)
// TODO: Allow cli to configure modules https://github.com/ChainSafe/ethermint/issues/74
modules := defaultModules()
whitelist := make(map[string]bool)
for _, module := range modules {
whitelist[module] = true
}
// Register all the APIs exposed by the services
for _, api := range apis {
if whitelist[api.Namespace] || (len(whitelist) == 0 && api.Public) {
if err := s.RegisterName(api.Namespace, api.Service); err != nil {
log.Println(err)
return
}
}
}
rs.Mux.HandleFunc("/rpc", s.ServeHTTP).Methods("POST")
}

View File

@ -1,6 +1,7 @@
package rpc
import (
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/ethermint/version"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
@ -10,11 +11,15 @@ import (
)
// PublicEthAPI is the eth_ prefixed set of APIs in the Web3 JSON-RPC spec.
type PublicEthAPI struct{}
type PublicEthAPI struct{
cliCtx context.CLIContext
}
// NewPublicEthAPI creates an instance of the public ETH Web3 API.
func NewPublicEthAPI() *PublicEthAPI {
return &PublicEthAPI{}
func NewPublicEthAPI(cliCtx context.CLIContext) *PublicEthAPI {
return &PublicEthAPI{
cliCtx: cliCtx,
}
}
// ProtocolVersion returns the supported Ethereum protocol version.

View File

@ -1,34 +1,13 @@
// Package rpc contains RPC handler methods and utilities to start
// Ethermint's Web3-compatibly JSON-RPC server.
package rpc
import (
"github.com/cosmos/ethermint/version"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/rpc"
)
// GetRPCAPIs returns the master list of public APIs for use with
// StartHTTPEndpoint.
func GetRPCAPIs() []rpc.API {
return []rpc.API{
{
Namespace: "web3",
Version: "1.0",
Service: NewPublicWeb3API(),
},
{
Namespace: "eth",
Version: "1.0",
Service: NewPublicEthAPI(),
},
}
}
// PublicWeb3API is the web3_ prefixed set of APIs in the Web3 JSON-RPC spec.
type PublicWeb3API struct {
}
type PublicWeb3API struct {}
// NewPublicWeb3API creates an instance of the Web3 API.
func NewPublicWeb3API() *PublicWeb3API {

View File

@ -1,16 +0,0 @@
package rpc
// Config contains configuration fields that determine the
// behavior of the RPC HTTP server.
type Config struct {
// EnableRPC defines whether or not to enable the RPC server
EnableRPC bool
// RPCAddr defines the IP address to listen on
RPCAddr string
// RPCPort defines the port to listen on
RPCPort int
// RPCCORSDomains defines list of domains to enable CORS headers for (used by browsers)
RPCCORSDomains []string
// RPCVhosts defines list of domains to listen on (useful if Tendermint is addressable via DNS)
RPCVHosts []string
}