Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Add query-only mode flag #1360

Merged
merged 17 commits into from
Oct 19, 2022
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (ledger) [#1277](https://github.com/evmos/ethermint/pull/1277) Add Ledger preprocessing transaction hook for EIP-712-signed Cosmos payloads.
* (rpc) [#1296](https://github.com/evmos/ethermint/pull/1296) Add RPC Backend unit tests.
* (rpc) [#1352](https://github.com/evmos/ethermint/pull/1352) Make the grpc queries run concurrently, don't block the consensus state machine.
* (cli) [#1360](https://github.com/evmos/ethermint/pull/1360) Introduce a new `query-only` flag, such that when enabled (grpc|jsonrpc|grpc,jsonrpc), will start the node in a query-only mode.

### Bug Fixes

Expand Down
2 changes: 2 additions & 0 deletions server/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const (
AppDBBackend = "app-db-backend"
)

const QueryOnly = "query-only"

// GRPC-related flags.
const (
GRPCEnable = "grpc.enable"
Expand Down
150 changes: 85 additions & 65 deletions server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"golang.org/x/exp/slices"
mmsqe marked this conversation as resolved.
Show resolved Hide resolved

"github.com/spf13/cobra"

Expand Down Expand Up @@ -148,6 +149,8 @@ which accepts a path for the resulting pprof file.
cmd.Flags().Uint64(server.FlagMinRetainBlocks, 0, "Minimum block height offset during ABCI commit to prune Tendermint blocks")
cmd.Flags().String(srvflags.AppDBBackend, "", "The type of database for application and snapshots databases")

cmd.Flags().String(srvflags.QueryOnly, "", "Start the node in query only mode without Tendermint process (grpc|jsonrpc|grpc,jsonrpc)")
mmsqe marked this conversation as resolved.
Show resolved Hide resolved

cmd.Flags().Bool(srvflags.GRPCEnable, true, "Define if the gRPC server should be enabled")
cmd.Flags().String(srvflags.GRPCAddress, serverconfig.DefaultGRPCAddress, "the gRPC server address to listen on")
cmd.Flags().Bool(srvflags.GRPCWebEnable, true, "Define if the gRPC-Web server should be enabled. (Note: gRPC must also be enabled.)")
Expand All @@ -156,7 +159,7 @@ which accepts a path for the resulting pprof file.
cmd.Flags().Bool(srvflags.RPCEnable, false, "Defines if Cosmos-sdk REST server should be enabled")
cmd.Flags().Bool(srvflags.EnabledUnsafeCors, false, "Defines if CORS should be enabled (unsafe - use it at your own risk)")

cmd.Flags().Bool(srvflags.JSONRPCEnable, true, "Define if the gRPC server should be enabled")
cmd.Flags().Bool(srvflags.JSONRPCEnable, true, "Define if the JSON-RPC server should be enabled")
cmd.Flags().StringSlice(srvflags.JSONRPCAPI, config.GetDefaultAPINamespaces(), "Defines a list of JSON-RPC namespaces that should be enabled")
cmd.Flags().String(srvflags.JSONRPCAddress, config.DefaultJSONRPCAddress, "the JSON-RPC server address to listen on")
cmd.Flags().String(srvflags.JSONWsAddress, config.DefaultJSONRPCWsAddress, "the JSON-RPC WS server address to listen on")
Expand Down Expand Up @@ -310,30 +313,46 @@ func startInProcess(ctx *server.Context, clientCtx client.Context, appCreator ty
}

genDocProvider := node.DefaultGenesisDocProviderFunc(cfg)
tmNode, err := node.NewNode(
cfg,
pvm.LoadOrGenFilePV(cfg.PrivValidatorKeyFile(), cfg.PrivValidatorStateFile()),
nodeKey,
proxy.NewLocalClientCreator(app),
genDocProvider,
node.DefaultDBProvider,
node.DefaultMetricsProvider(cfg.Instrumentation),
ctx.Logger.With("server", "node"),
var (
tmNode *node.Node
modes = strings.Split(ctx.Viper.GetString(srvflags.QueryOnly), ",")
mmsqe marked this conversation as resolved.
Show resolved Hide resolved
)
if err != nil {
logger.Error("failed init node", "error", err.Error())
return err
}
gRPCQueryMode := slices.Contains(modes, "grpc")
jsonRPCMode := slices.Contains(modes, "jsonrpc")
if gRPCQueryMode || jsonRPCMode {
mmsqe marked this conversation as resolved.
Show resolved Hide resolved
ctx.Logger.Info(fmt.Sprintf("starting node in %s query only mode; Tendermint is disabled", modes))
if gRPCQueryMode {
config.GRPC.Enable = true
}
if jsonRPCMode {
config.JSONRPC.Enable = true
}
mmsqe marked this conversation as resolved.
Show resolved Hide resolved
} else {
tmNode, err = node.NewNode(
cfg,
pvm.LoadOrGenFilePV(cfg.PrivValidatorKeyFile(), cfg.PrivValidatorStateFile()),
nodeKey,
proxy.NewLocalClientCreator(app),
genDocProvider,
node.DefaultDBProvider,
node.DefaultMetricsProvider(cfg.Instrumentation),
ctx.Logger.With("server", "node"),
)
if err != nil {
logger.Error("failed init node", "error", err.Error())
return err
}

if err := tmNode.Start(); err != nil {
logger.Error("failed start tendermint server", "error", err.Error())
return err
if err := tmNode.Start(); err != nil {
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
logger.Error("failed start tendermint server", "error", err.Error())
return err
}
}

// Add the tx service to the gRPC router. We only need to register this
// service if API or gRPC or JSONRPC is enabled, and avoid doing so in the general
// case, because it spawns a new local tendermint RPC client.
if config.API.Enable || config.GRPC.Enable || config.JSONRPC.Enable || config.JSONRPC.EnableIndexer {
if (config.API.Enable || config.GRPC.Enable || config.JSONRPC.Enable || config.JSONRPC.EnableIndexer) && tmNode != nil {
clientCtx = clientCtx.WithClient(local.New(tmNode))

app.RegisterTxService(clientCtx)
Expand Down Expand Up @@ -443,15 +462,62 @@ func startInProcess(ctx *server.Context, clientCtx client.Context, appCreator ty
if err != nil {
return err
}
defer grpcSrv.Stop()
if config.GRPCWeb.Enable {
grpcWebSrv, err = servergrpc.StartGRPCWeb(grpcSrv, config.Config)
if err != nil {
ctx.Logger.Error("failed to start grpc-web http server", "error", err)
return err
}
defer func() {
if err := grpcWebSrv.Close(); err != nil {
logger.Error("failed to close the grpcWebSrc", "error", err.Error())
}
}()
}
}

var (
httpSrv *http.Server
httpSrvDone chan struct{}
)

if config.JSONRPC.Enable {
genDoc, err := genDocProvider()
if err != nil {
return err
}

clientCtx := clientCtx.WithChainID(genDoc.ChainID)

tmEndpoint := "/websocket"
tmRPCAddr := cfg.RPC.ListenAddress
httpSrv, httpSrvDone, err = StartJSONRPC(ctx, clientCtx, tmRPCAddr, tmEndpoint, &config, idxer)
if err != nil {
return err
}
defer func() {
shutdownCtx, cancelFn := context.WithTimeout(context.Background(), 10*time.Second)
defer cancelFn()
if err := httpSrv.Shutdown(shutdownCtx); err != nil {
logger.Error("HTTP server shutdown produced a warning", "error", err.Error())
} else {
logger.Info("HTTP server shut down, waiting 5 sec")
select {
case <-time.Tick(5 * time.Second):
case <-httpSrvDone:
}
}
}()
}

// At this point it is safe to block the process if we're in gRPC or JSON-RPC only mode as
// we do not need to start Rosetta or handle any Tendermint related processes.
if gRPCQueryMode || jsonRPCMode {
// wait for signal capture and gracefully return
return server.WaitForQuitSignals()
}

var rosettaSrv crgserver.Server
if config.Rosetta.Enable {
offlineMode := config.Rosetta.Offline
Expand Down Expand Up @@ -488,29 +554,8 @@ func startInProcess(ctx *server.Context, clientCtx client.Context, appCreator ty
}
}

var (
httpSrv *http.Server
httpSrvDone chan struct{}
)

if config.JSONRPC.Enable {
genDoc, err := genDocProvider()
if err != nil {
return err
}

clientCtx := clientCtx.WithChainID(genDoc.ChainID)

tmEndpoint := "/websocket"
tmRPCAddr := cfg.RPC.ListenAddress
httpSrv, httpSrvDone, err = StartJSONRPC(ctx, clientCtx, tmRPCAddr, tmEndpoint, &config, idxer)
if err != nil {
return err
}
}

defer func() {
mmsqe marked this conversation as resolved.
Show resolved Hide resolved
if tmNode.IsRunning() {
if tmNode != nil && tmNode.IsRunning() {
_ = tmNode.Stop()
}

Expand All @@ -521,31 +566,6 @@ func startInProcess(ctx *server.Context, clientCtx client.Context, appCreator ty
if apiSrv != nil {
_ = apiSrv.Close()
}

if grpcSrv != nil {
grpcSrv.Stop()
if grpcWebSrv != nil {
if err := grpcWebSrv.Close(); err != nil {
logger.Error("failed to close the grpcWebSrc", "error", err.Error())
}
}
}

if httpSrv != nil {
shutdownCtx, cancelFn := context.WithTimeout(context.Background(), 10*time.Second)
defer cancelFn()

if err := httpSrv.Shutdown(shutdownCtx); err != nil {
logger.Error("HTTP server shutdown produced a warning", "error", err.Error())
} else {
logger.Info("HTTP server shut down, waiting 5 sec")
select {
case <-time.Tick(5 * time.Second):
case <-httpSrvDone:
}
}
}

logger.Info("Bye!")
}()

Expand Down