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"
|
|
|
|
ethrpc "github.com/ethereum/go-ethereum/rpc"
|
2021-10-01 14:49:22 +00:00
|
|
|
"github.com/tharsis/ethermint/rpc"
|
2021-07-28 09:47:29 +00:00
|
|
|
|
|
|
|
"github.com/tharsis/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
|
|
|
|
|
|
|
rpcServer := ethrpc.NewServer()
|
2021-07-26 11:15:55 +00:00
|
|
|
|
2021-08-16 09:45:10 +00:00
|
|
|
rpcAPIArr := config.JSONRPC.API
|
2021-07-26 11:15:55 +00:00
|
|
|
apis := rpc.GetRPCAPIs(ctx, clientCtx, tmWsClient, 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{
|
2021-08-16 09:45:10 +00:00
|
|
|
Addr: config.JSONRPC.Address,
|
2021-07-23 12:31:59 +00:00
|
|
|
Handler: handlerWithCors.Handler(r),
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|