laconicd/server/evmrpc.go
crypto-facs d1212725f8
feat: configurable JSON-RPC APIs (#349)
* fix confusing name

* feat: Enable configurable grpc apis

* docs: Update docs and changelog

* Organized flags order

* fix linter

* fix linter

* fix maligned struct

* fix typo in docs

* fix unnecesary duplicate

* Update cmd/ethermintd/config/config.go

Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>

* Update cmd/ethermintd/config/config.go

Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>

* Update cmd/ethermintd/config/config.go

Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>

* Update docs/basics/json_rpc.md

Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>

* fix eth to be manage as default

* Update init.sh

Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>

* Update tests/solidity/init-test-node.sh

Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>

* add default case

* add default enable api namespaces

* update changelog

* fix namespaces array  handler

* remove duplicated changelog

* fix typo

* remove duplicates namespaces and fix eth namespace issue

* fix variable name

* break line in docs

Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>
2021-07-26 11:15:55 +00:00

83 lines
2.3 KiB
Go

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()
rpcAPIArr := config.EVMRPC.API
apis := rpc.GetRPCAPIs(ctx, clientCtx, tmWsClient, rpcAPIArr)
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
}