2022-12-16 09:48:38 +00:00
|
|
|
// Copyright 2021 Evmos Foundation
|
|
|
|
// This file is part of Evmos' Ethermint library.
|
|
|
|
//
|
|
|
|
// The Ethermint library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The Ethermint library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the Ethermint library. If not, see https://github.com/evmos/ethermint/blob/main/LICENSE
|
2021-06-25 09:18:37 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2022-10-10 10:38:33 +00:00
|
|
|
"net"
|
2021-06-25 09:18:37 +00:00
|
|
|
"net/http"
|
2022-04-23 14:37:00 +00:00
|
|
|
"time"
|
2021-06-25 09:18:37 +00:00
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
"github.com/cerc-io/laconicd/server/config"
|
2021-06-25 09:18:37 +00:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/improbable-eng/grpc-web/go/grpcweb"
|
2021-06-29 17:02:21 +00:00
|
|
|
"github.com/spf13/cobra"
|
2022-10-10 10:38:33 +00:00
|
|
|
"golang.org/x/net/netutil"
|
2021-06-29 17:02:21 +00:00
|
|
|
|
|
|
|
sdkserver "github.com/cosmos/cosmos-sdk/server"
|
|
|
|
"github.com/cosmos/cosmos-sdk/server/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/version"
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
tmcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
|
2021-08-19 16:55:13 +00:00
|
|
|
tmlog "github.com/tendermint/tendermint/libs/log"
|
2021-06-29 17:02:21 +00:00
|
|
|
rpcclient "github.com/tendermint/tendermint/rpc/jsonrpc/client"
|
2021-06-25 09:18:37 +00:00
|
|
|
)
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
// AddCommands adds server commands
|
|
|
|
func AddCommands(
|
|
|
|
rootCmd *cobra.Command,
|
2023-01-20 16:36:23 +00:00
|
|
|
opts StartOptions,
|
2022-10-10 10:38:33 +00:00
|
|
|
appExport types.AppExporter,
|
|
|
|
addStartFlags types.ModuleInitFlags,
|
|
|
|
) {
|
2021-06-29 17:02:21 +00:00
|
|
|
tendermintCmd := &cobra.Command{
|
|
|
|
Use: "tendermint",
|
|
|
|
Short: "Tendermint subcommands",
|
|
|
|
}
|
|
|
|
|
|
|
|
tendermintCmd.AddCommand(
|
|
|
|
sdkserver.ShowNodeIDCmd(),
|
|
|
|
sdkserver.ShowValidatorCmd(),
|
|
|
|
sdkserver.ShowAddressCmd(),
|
|
|
|
sdkserver.VersionCmd(),
|
2022-06-06 12:37:13 +00:00
|
|
|
tmcmd.ResetAllCmd,
|
|
|
|
tmcmd.ResetStateCmd,
|
2021-06-29 17:02:21 +00:00
|
|
|
)
|
|
|
|
|
2023-01-20 16:36:23 +00:00
|
|
|
startCmd := StartCmd(opts)
|
2021-06-29 17:02:21 +00:00
|
|
|
addStartFlags(startCmd)
|
|
|
|
|
|
|
|
rootCmd.AddCommand(
|
|
|
|
startCmd,
|
|
|
|
tendermintCmd,
|
2023-01-20 16:36:23 +00:00
|
|
|
sdkserver.ExportCmd(appExport, opts.DefaultNodeHome),
|
2021-06-29 17:02:21 +00:00
|
|
|
version.NewVersionCommand(),
|
2023-01-20 16:36:23 +00:00
|
|
|
sdkserver.NewRollbackCmd(opts.AppCreator, opts.DefaultNodeHome),
|
2022-10-10 10:38:33 +00:00
|
|
|
|
|
|
|
// custom tx indexer command
|
|
|
|
NewIndexTxCmd(),
|
2021-06-29 17:02:21 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-08-19 16:55:13 +00:00
|
|
|
func ConnectTmWS(tmRPCAddr, tmEndpoint string, logger tmlog.Logger) *rpcclient.WSClient {
|
2022-09-30 06:41:39 +00:00
|
|
|
tmWsClient, err := rpcclient.NewWS(tmRPCAddr, tmEndpoint,
|
|
|
|
rpcclient.MaxReconnectAttempts(256),
|
|
|
|
rpcclient.ReadWait(120*time.Second),
|
|
|
|
rpcclient.WriteWait(120*time.Second),
|
|
|
|
rpcclient.PingPeriod(50*time.Second),
|
|
|
|
rpcclient.OnReconnect(func() {
|
|
|
|
logger.Debug("EVM RPC reconnects to Tendermint WS", "address", tmRPCAddr+tmEndpoint)
|
|
|
|
}),
|
|
|
|
)
|
2022-04-23 14:37:00 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(
|
|
|
|
"Tendermint WS client could not be created",
|
|
|
|
"address", tmRPCAddr+tmEndpoint,
|
|
|
|
"error", err,
|
|
|
|
)
|
2022-09-30 06:41:39 +00:00
|
|
|
} else if err := tmWsClient.OnStart(); err != nil {
|
2022-04-23 14:37:00 +00:00
|
|
|
logger.Error(
|
|
|
|
"Tendermint WS client could not start",
|
|
|
|
"address", tmRPCAddr+tmEndpoint,
|
|
|
|
"error", err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tmWsClient
|
2021-06-25 09:18:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func MountGRPCWebServices(
|
|
|
|
router *mux.Router,
|
|
|
|
grpcWeb *grpcweb.WrappedGrpcServer,
|
|
|
|
grpcResources []string,
|
2021-08-19 16:55:13 +00:00
|
|
|
logger tmlog.Logger,
|
2021-06-25 09:18:37 +00:00
|
|
|
) {
|
|
|
|
for _, res := range grpcResources {
|
2021-08-19 16:55:13 +00:00
|
|
|
logger.Info("[GRPC Web] HTTP POST mounted", "resource", res)
|
2021-06-25 09:18:37 +00:00
|
|
|
|
|
|
|
s := router.Methods("POST").Subrouter()
|
|
|
|
s.HandleFunc(res, func(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
if grpcWeb.IsGrpcWebSocketRequest(req) {
|
|
|
|
grpcWeb.HandleGrpcWebsocketRequest(resp, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if grpcWeb.IsGrpcWebRequest(req) {
|
|
|
|
grpcWeb.HandleGrpcWebRequest(resp, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-10-10 10:38:33 +00:00
|
|
|
|
|
|
|
// Listen starts a net.Listener on the tcp network on the given address.
|
|
|
|
// If there is a specified MaxOpenConnections in the config, it will also set the limitListener.
|
|
|
|
func Listen(addr string, config *config.Config) (net.Listener, error) {
|
|
|
|
if addr == "" {
|
|
|
|
addr = ":http"
|
|
|
|
}
|
|
|
|
ln, err := net.Listen("tcp", addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if config.JSONRPC.MaxOpenConnections > 0 {
|
|
|
|
ln = netutil.LimitListener(ln, config.JSONRPC.MaxOpenConnections)
|
|
|
|
}
|
|
|
|
return ln, err
|
|
|
|
}
|