Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
server: sync start command with cosmos-sdk v0.43-rc2 (#341)
Browse files Browse the repository at this point in the history
* sync start command with cosmos-sdk 0.43

Closes #340

- add grpc web and rosetta server
- add tx/tm services
- add standalone mode
- update cosmos-sdk to 0.43.0-rc2, which fixed a bug in service startup
- add EnableUnsafeCORS option to evm rpc server

* set keyring options in root cmd and use viper prefix

* fix linter and pr suggestions

Co-authored-by: Federico Kunze Küllmer <[email protected]>
  • Loading branch information
yihuang and fedekunze authored Jul 23, 2021
1 parent 3f136bf commit e61594e
Show file tree
Hide file tree
Showing 6 changed files with 292 additions and 139 deletions.
10 changes: 6 additions & 4 deletions cmd/ethermintd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,14 @@ func DefaultEVMConfig() *EVMRPCConfig {

// EVMRPCConfig defines configuration for the EVM RPC server.
type EVMRPCConfig struct {
// Enable defines if the EVM RPC server should be enabled.
Enable bool `mapstructure:"enable"`
// Address defines the HTTP server to listen on
// RPCAddress defines the HTTP server to listen on
RPCAddress string `mapstructure:"address"`
// Address defines the WebSocket server to listen on
// WsAddress defines the WebSocket server to listen on
WsAddress string `mapstructure:"ws-address"`
// Enable defines if the EVM RPC server should be enabled.
Enable bool `mapstructure:"enable"`
// EnableUnsafeCORS defines if CORS should be enabled (unsafe - use it at your own risk)
EnableUnsafeCORS bool `mapstructure:"enable-unsafe-cors"`
}

// Config defines the server's top level configuration. It includes the default app config
Expand Down
6 changes: 5 additions & 1 deletion cmd/ethermintd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,13 @@ import (
"github.com/tharsis/ethermint/app"
ethermintclient "github.com/tharsis/ethermint/client"
ethermintconfig "github.com/tharsis/ethermint/cmd/ethermintd/config"
"github.com/tharsis/ethermint/crypto/hd"
"github.com/tharsis/ethermint/encoding"
"github.com/tharsis/ethermint/server"
)

const EnvPrefix = "ETHERMINT"

// NewRootCmd creates a new root command for simd. It is called once in the
// main function.
func NewRootCmd() (*cobra.Command, params.EncodingConfig) {
Expand All @@ -52,7 +55,8 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) {
WithAccountRetriever(types.AccountRetriever{}).
WithBroadcastMode(flags.BroadcastBlock).
WithHomeDir(app.DefaultNodeHome).
WithViper("") // In simapp, we don't use any prefix for env variables.
WithKeyringOptions(hd.EthSecp256k1Option()).
WithViper(EnvPrefix)

rootCmd := &cobra.Command{
Use: "ethermintd",
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/bugsnag/bugsnag-go v2.1.0+incompatible // indirect
github.com/bugsnag/panicwrap v1.3.2 // indirect
github.com/cespare/cp v1.1.1 // indirect
github.com/cosmos/cosmos-sdk v0.43.0-rc1
github.com/cosmos/cosmos-sdk v0.43.0-rc2
github.com/cosmos/go-bip39 v1.0.0
github.com/cosmos/ibc-go v1.0.0-beta1
github.com/deckarep/golang-set v1.7.1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSV
github.com/coreos/pkg v0.0.0-20160727233714-3ac0863d7acf/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/cosmos/cosmos-sdk v0.43.0-beta1/go.mod h1:rpCPaC3MnityU4Io4CDZqZB4GMtPqNeYXxPk8iRqmYM=
github.com/cosmos/cosmos-sdk v0.43.0-rc1 h1:3QGgMqwLmzW+015P4ZEIQ+wRj7TrVU063D2QpHc2Syw=
github.com/cosmos/cosmos-sdk v0.43.0-rc1/go.mod h1:ctcrTEAhei9s8O3KSNvL0dxe+fVQGp07QyRb/7H9JYE=
github.com/cosmos/cosmos-sdk v0.43.0-rc2 h1:9xww4vDnsNyZyF1p9U4zpc8tc5Ctx763WQWLccddP8A=
github.com/cosmos/cosmos-sdk v0.43.0-rc2/go.mod h1:ctcrTEAhei9s8O3KSNvL0dxe+fVQGp07QyRb/7H9JYE=
github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d/go.mod h1:tSxLoYXyBmiFeKpvmq4dzayMdCjCnu8uqmCysIGBT2Y=
github.com/cosmos/go-bip39 v1.0.0 h1:pcomnQdrdH22njcAatO0yWojsUnCO3y2tNoV1cb6hHY=
github.com/cosmos/go-bip39 v1.0.0/go.mod h1:RNJv0H/pOIVgxw6KS7QeX2a0Uo0aKUlfhZ4xuwvCdJw=
Expand Down
80 changes: 80 additions & 0 deletions server/evmrpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package server

import (
"net"
"net/http"
"time"

"github.com/gorilla/mux"
"github.com/rs/cors"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/server/types"
ethrpc "github.com/ethereum/go-ethereum/rpc"
"github.com/tharsis/ethermint/cmd/ethermintd/config"
"github.com/tharsis/ethermint/ethereum/rpc"
)

// StartEVMRPC start evm rpc server
func StartEVMRPC(ctx *server.Context, clientCtx client.Context, tmRPCAddr string, tmEndpoint string, config config.Config) (*http.Server, chan struct{}, error) {
tmWsClient := ConnectTmWS(tmRPCAddr, tmEndpoint)

rpcServer := ethrpc.NewServer()
apis := rpc.GetRPCAPIs(ctx, clientCtx, tmWsClient)

for _, api := range apis {
if err := rpcServer.RegisterName(api.Namespace, api.Service); err != nil {
ctx.Logger.Error(
"failed to register service in EVM RPC namespace",
"namespace", api.Namespace,
"service", api.Service,
)
return nil, nil, err
}
}

r := mux.NewRouter()
r.HandleFunc("/", rpcServer.ServeHTTP).Methods("POST")

handlerWithCors := cors.Default()
if config.EVMRPC.EnableUnsafeCORS {
handlerWithCors = cors.AllowAll()
}

httpSrv := &http.Server{
Addr: config.EVMRPC.RPCAddress,
Handler: handlerWithCors.Handler(r),
}
httpSrvDone := make(chan struct{}, 1)

errCh := make(chan error)
go func() {
ctx.Logger.Info("Starting EVM RPC server", "address", config.EVMRPC.RPCAddress)
if err := httpSrv.ListenAndServe(); err != nil {
if err == http.ErrServerClosed {
close(httpSrvDone)
return
}

ctx.Logger.Error("failed to start EVM RPC server", "error", err.Error())
errCh <- err
}
}()

select {
case err := <-errCh:
ctx.Logger.Error("failed to boot EVM RPC server", "error", err.Error())
return nil, nil, err
case <-time.After(types.ServerStartTime): // assume EVM RPC server started successfully
}

ctx.Logger.Info("Starting EVM WebSocket server", "address", config.EVMRPC.WsAddress)
_, port, _ := net.SplitHostPort(config.EVMRPC.RPCAddress)

// allocate separate WS connection to Tendermint
tmWsClient = ConnectTmWS(tmRPCAddr, tmEndpoint)
wsSrv := rpc.NewWebsocketsServer(ctx.Logger, tmWsClient, "localhost:"+port, config.EVMRPC.WsAddress)
wsSrv.Start()
return httpSrv, httpSrvDone, nil
}
Loading

0 comments on commit e61594e

Please sign in to comment.