2021-06-25 09:18:37 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2022-04-23 14:37:00 +00:00
|
|
|
"time"
|
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"
|
|
|
|
|
|
|
|
sdkserver "github.com/cosmos/cosmos-sdk/server"
|
|
|
|
"github.com/cosmos/cosmos-sdk/server/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/version"
|
2022-06-06 12:37:13 +00:00
|
|
|
tmcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
|
2021-06-29 17:02:21 +00:00
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2021-06-29 17:02:21 +00:00
|
|
|
// add server commands
|
|
|
|
func AddCommands(rootCmd *cobra.Command, defaultNodeHome string, appCreator types.AppCreator, appExport types.AppExporter, addStartFlags types.ModuleInitFlags) {
|
|
|
|
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,
|
|
|
|
tmcmd.InspectCmd,
|
2021-06-29 17:02:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
startCmd := StartCmd(appCreator, defaultNodeHome)
|
|
|
|
addStartFlags(startCmd)
|
|
|
|
|
|
|
|
rootCmd.AddCommand(
|
|
|
|
startCmd,
|
|
|
|
tendermintCmd,
|
2021-09-15 08:27:37 +00:00
|
|
|
sdkserver.ExportCmd(appExport, defaultNodeHome),
|
2021-06-29 17:02:21 +00:00
|
|
|
version.NewVersionCommand(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-08-19 16:55:13 +00:00
|
|
|
func ConnectTmWS(tmRPCAddr, tmEndpoint string, logger tmlog.Logger) *rpcclient.WSClient {
|
2022-04-23 14:37:00 +00:00
|
|
|
tmWsClient, err := rpcclient.NewWSWithOptions(tmRPCAddr, tmEndpoint, rpcclient.WSOptions{
|
|
|
|
MaxReconnectAttempts: 256, // first: 2 sec, last: 17 min.
|
|
|
|
WriteWait: 120 * time.Second,
|
|
|
|
ReadWait: 120 * time.Second,
|
|
|
|
PingPeriod: 50 * time.Second,
|
|
|
|
})
|
|
|
|
|
|
|
|
tmWsClient.OnReconnect(func() {
|
|
|
|
logger.Debug("EVM RPC reconnects to Tendermint WS", "address", tmRPCAddr+tmEndpoint)
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(
|
|
|
|
"Tendermint WS client could not be created",
|
|
|
|
"address", tmRPCAddr+tmEndpoint,
|
|
|
|
"error", err,
|
|
|
|
)
|
|
|
|
} else if err := tmWsClient.Start(); err != nil {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|