2021-07-23 12:31:59 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
2022-02-19 00:49:34 +00:00
|
|
|
ethlog "github.com/ethereum/go-ethereum/log"
|
2021-07-23 12:31:59 +00:00
|
|
|
ethrpc "github.com/ethereum/go-ethereum/rpc"
|
2022-06-19 09:43:41 +00:00
|
|
|
"github.com/evmos/ethermint/rpc"
|
2021-07-28 09:47:29 +00:00
|
|
|
|
2022-06-19 09:43:41 +00:00
|
|
|
"github.com/evmos/ethermint/server/config"
|
2021-07-23 12:31:59 +00:00
|
|
|
)
|
|
|
|
|
2021-08-16 09:45:10 +00:00
|
|
|
// StartJSONRPC starts the JSON-RPC server
|
2021-09-05 11:03:06 +00:00
|
|
|
func StartJSONRPC(ctx *server.Context, clientCtx client.Context, tmRPCAddr, tmEndpoint string, config config.Config) (*http.Server, chan struct{}, error) {
|
2021-08-19 16:55:13 +00:00
|
|
|
tmWsClient := ConnectTmWS(tmRPCAddr, tmEndpoint, ctx.Logger)
|
2021-07-23 12:31:59 +00:00
|
|
|
|
2022-02-19 00:49:34 +00:00
|
|
|
logger := ctx.Logger.With("module", "geth")
|
|
|
|
ethlog.Root().SetHandler(ethlog.FuncHandler(func(r *ethlog.Record) error {
|
|
|
|
switch r.Lvl {
|
|
|
|
case ethlog.LvlTrace, ethlog.LvlDebug:
|
|
|
|
logger.Debug(r.Msg, r.Ctx...)
|
|
|
|
case ethlog.LvlInfo, ethlog.LvlWarn:
|
|
|
|
logger.Info(r.Msg, r.Ctx...)
|
|
|
|
case ethlog.LvlError, ethlog.LvlCrit:
|
|
|
|
logger.Error(r.Msg, r.Ctx...)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}))
|
|
|
|
|
2021-07-23 12:31:59 +00:00
|
|
|
rpcServer := ethrpc.NewServer()
|
2021-07-26 11:15:55 +00:00
|
|
|
|
2022-06-22 10:50:36 +00:00
|
|
|
allowUnprotectedTxs := config.JSONRPC.AllowUnprotectedTxs
|
2021-08-16 09:45:10 +00:00
|
|
|
rpcAPIArr := config.JSONRPC.API
|
2022-06-22 10:50:36 +00:00
|
|
|
|
|
|
|
apis := rpc.GetRPCAPIs(ctx, clientCtx, tmWsClient, allowUnprotectedTxs, rpcAPIArr)
|
2021-07-23 12:31:59 +00:00
|
|
|
|
|
|
|
for _, api := range apis {
|
|
|
|
if err := rpcServer.RegisterName(api.Namespace, api.Service); err != nil {
|
|
|
|
ctx.Logger.Error(
|
2021-08-16 09:45:10 +00:00
|
|
|
"failed to register service in JSON RPC namespace",
|
2021-07-23 12:31:59 +00:00
|
|
|
"namespace", api.Namespace,
|
|
|
|
"service", api.Service,
|
|
|
|
)
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r := mux.NewRouter()
|
|
|
|
r.HandleFunc("/", rpcServer.ServeHTTP).Methods("POST")
|
|
|
|
|
|
|
|
handlerWithCors := cors.Default()
|
2021-08-20 10:58:03 +00:00
|
|
|
if config.API.EnableUnsafeCORS {
|
2021-07-23 12:31:59 +00:00
|
|
|
handlerWithCors = cors.AllowAll()
|
|
|
|
}
|
|
|
|
|
|
|
|
httpSrv := &http.Server{
|
2022-07-19 15:00:43 +00:00
|
|
|
Addr: config.JSONRPC.Address,
|
|
|
|
Handler: handlerWithCors.Handler(r),
|
|
|
|
ReadHeaderTimeout: config.JSONRPC.HTTPTimeout,
|
|
|
|
ReadTimeout: config.JSONRPC.HTTPTimeout,
|
|
|
|
WriteTimeout: config.JSONRPC.HTTPTimeout,
|
|
|
|
IdleTimeout: config.JSONRPC.HTTPIdleTimeout,
|
2021-07-23 12:31:59 +00:00
|
|
|
}
|
|
|
|
httpSrvDone := make(chan struct{}, 1)
|
|
|
|
|
|
|
|
errCh := make(chan error)
|
|
|
|
go func() {
|
2021-08-16 09:45:10 +00:00
|
|
|
ctx.Logger.Info("Starting JSON-RPC server", "address", config.JSONRPC.Address)
|
2021-07-23 12:31:59 +00:00
|
|
|
if err := httpSrv.ListenAndServe(); err != nil {
|
|
|
|
if err == http.ErrServerClosed {
|
|
|
|
close(httpSrvDone)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-16 09:45:10 +00:00
|
|
|
ctx.Logger.Error("failed to start JSON-RPC server", "error", err.Error())
|
2021-07-23 12:31:59 +00:00
|
|
|
errCh <- err
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-errCh:
|
2021-08-16 09:45:10 +00:00
|
|
|
ctx.Logger.Error("failed to boot JSON-RPC server", "error", err.Error())
|
2021-07-23 12:31:59 +00:00
|
|
|
return nil, nil, err
|
2021-08-16 09:45:10 +00:00
|
|
|
case <-time.After(types.ServerStartTime): // assume JSON RPC server started successfully
|
2021-07-23 12:31:59 +00:00
|
|
|
}
|
|
|
|
|
2021-08-16 09:45:10 +00:00
|
|
|
ctx.Logger.Info("Starting JSON WebSocket server", "address", config.JSONRPC.WsAddress)
|
2021-07-23 12:31:59 +00:00
|
|
|
|
|
|
|
// allocate separate WS connection to Tendermint
|
2021-08-19 16:55:13 +00:00
|
|
|
tmWsClient = ConnectTmWS(tmRPCAddr, tmEndpoint, ctx.Logger)
|
2022-01-13 13:12:57 +00:00
|
|
|
wsSrv := rpc.NewWebsocketsServer(clientCtx, ctx.Logger, tmWsClient, config)
|
2021-07-23 12:31:59 +00:00
|
|
|
wsSrv.Start()
|
|
|
|
return httpSrv, httpSrvDone, nil
|
|
|
|
}
|