Skip to content
This repository was archived by the owner on Nov 30, 2021. It is now read-only.
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
9 changes: 5 additions & 4 deletions cmd/emintcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()
Expand All @@ -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,
Expand Down
24 changes: 24 additions & 0 deletions rpc/apis.go
Original file line number Diff line number Diff line change
@@ -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),
},
}
}
3 changes: 2 additions & 1 deletion server/rpc/apis_test.go → rpc/apis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rpc

import (
"context"
sdkcontext"github.com/cosmos/cosmos-sdk/client/context"
"testing"
"time"

Expand Down Expand Up @@ -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
}
Expand Down
60 changes: 60 additions & 0 deletions rpc/config.go
Original file line number Diff line number Diff line change
@@ -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")
}
11 changes: 8 additions & 3 deletions server/rpc/eth_api.go → rpc/eth_api.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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.
Expand Down
File renamed without changes.
File renamed without changes.
23 changes: 1 addition & 22 deletions server/rpc/apis.go → rpc/web3_api.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
16 changes: 0 additions & 16 deletions server/rpc/config.go

This file was deleted.