Skip to content

Commit

Permalink
refactor(server/v2): simplify servers (cosmos#20796)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt authored Jun 27, 2024
1 parent be0e2ee commit afddef3
Show file tree
Hide file tree
Showing 25 changed files with 269 additions and 256 deletions.
6 changes: 3 additions & 3 deletions client/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ func SetCmdClientContext(cmd *cobra.Command, clientCtx Context) error {
}

func GetViperFromCmd(cmd *cobra.Command) *viper.Viper {
value := cmd.Context().Value(corectx.ViperContextKey{})
value := cmd.Context().Value(corectx.ViperContextKey)
v, ok := value.(*viper.Viper)
if !ok {
return viper.New()
Expand All @@ -388,7 +388,7 @@ func GetViperFromCmd(cmd *cobra.Command) *viper.Viper {
}

func GetConfigFromCmd(cmd *cobra.Command) *cmtcfg.Config {
v := cmd.Context().Value(corectx.ViperContextKey{})
v := cmd.Context().Value(corectx.ViperContextKey)
viper, ok := v.(*viper.Viper)
if !ok {
return cmtcfg.DefaultConfig()
Expand All @@ -397,7 +397,7 @@ func GetConfigFromCmd(cmd *cobra.Command) *cmtcfg.Config {
}

func GetLoggerFromCmd(cmd *cobra.Command) log.Logger {
v := cmd.Context().Value(corectx.LoggerContextKey{})
v := cmd.Context().Value(corectx.LoggerContextKey)
logger, ok := v.(log.Logger)
if !ok {
return log.NewLogger(cmd.OutOrStdout())
Expand Down
9 changes: 7 additions & 2 deletions core/context/server_context.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package context

type (
LoggerContextKey struct{}
ViperContextKey struct{}
loggerContextKey struct{}
viperContextKey struct{}
)

var (
LoggerContextKey loggerContextKey
ViperContextKey viperContextKey
)
4 changes: 2 additions & 2 deletions server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ func SetCmdServerContext(cmd *cobra.Command, serverCtx *Context) error {
}

cmdCtx = context.WithValue(cmdCtx, ServerContextKey, serverCtx)
cmdCtx = context.WithValue(cmdCtx, corectx.ViperContextKey{}, serverCtx.Viper)
cmdCtx = context.WithValue(cmdCtx, corectx.LoggerContextKey{}, serverCtx.Logger)
cmdCtx = context.WithValue(cmdCtx, corectx.ViperContextKey, serverCtx.Viper)
cmdCtx = context.WithValue(cmdCtx, corectx.LoggerContextKey, serverCtx.Logger)

cmd.SetContext(cmdCtx)

Expand Down
8 changes: 3 additions & 5 deletions server/v2/api/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (
"cosmossdk.io/server/v2/api/grpc/gogoreflection"
)

const serverName = "grpc-server"

type GRPCServer[AppT serverv2.AppI[T], T transaction.Tx] struct {
logger log.Logger
config *Config
Expand All @@ -44,7 +42,7 @@ func New[AppT serverv2.AppI[T], T transaction.Tx]() *GRPCServer[AppT, T] {
func (g *GRPCServer[AppT, T]) Init(appI AppT, v *viper.Viper, logger log.Logger) error {
cfg := DefaultConfig()
if v != nil {
if err := v.Sub(serverName).Unmarshal(&cfg); err != nil {
if err := v.Sub(g.Name()).Unmarshal(&cfg); err != nil {
return fmt.Errorf("failed to unmarshal config: %w", err)
}
}
Expand All @@ -63,13 +61,13 @@ func (g *GRPCServer[AppT, T]) Init(appI AppT, v *viper.Viper, logger log.Logger)

g.grpcSrv = grpcSrv
g.config = cfg
g.logger = logger.With(log.ModuleKey, serverName)
g.logger = logger.With(log.ModuleKey, g.Name())

return nil
}

func (g *GRPCServer[AppT, T]) Name() string {
return serverName
return "grpc-server"
}

func (g *GRPCServer[AppT, T]) Start(ctx context.Context) error {
Expand Down
20 changes: 0 additions & 20 deletions server/v2/cometbft/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@ func NewConsensus[T transaction.Tx](
}
}

func (c *Consensus[T]) SetMempool(mp mempool.Mempool[T]) {
c.mempool = mp
}

func (c *Consensus[T]) SetStreamingManager(sm streaming.Manager) {
c.streaming = sm
}
Expand All @@ -94,22 +90,6 @@ func (c *Consensus[T]) RegisterExtensions(extensions ...snapshots.ExtensionSnaps
}
}

func (c *Consensus[T]) SetPrepareProposalHandler(handler handlers.PrepareHandler[T]) {
c.prepareProposalHandler = handler
}

func (c *Consensus[T]) SetProcessProposalHandler(handler handlers.ProcessHandler[T]) {
c.processProposalHandler = handler
}

func (c *Consensus[T]) SetExtendVoteExtension(handler handlers.ExtendVoteHandler) {
c.extendVote = handler
}

func (c *Consensus[T]) SetVerifyVoteExtension(handler handlers.VerifyVoteExtensionhandler) {
c.verifyVoteExt = handler
}

// BlockData is used to keep some data about the last committed block. Currently
// we only use the height, the rest is not needed right now and might get removed
// in the future.
Expand Down
27 changes: 13 additions & 14 deletions server/v2/cometbft/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"sigs.k8s.io/yaml"

"cosmossdk.io/server/v2/cometbft/client/rpc"
"cosmossdk.io/server/v2/cometbft/flags"
auth "cosmossdk.io/x/auth/client/cli"
"github.com/cosmos/cosmos-sdk/client"

Expand All @@ -38,8 +37,8 @@ func (s *CometBFTServer[AppT, T]) rpcClient(cmd *cobra.Command) (rpc.CometRPC, e
return client, nil
}

if s.Node == nil || cmd.Flags().Changed(flags.FlagNode) {
rpcURI, err := cmd.Flags().GetString(flags.FlagNode)
if s.Node == nil || cmd.Flags().Changed(FlagNode) {
rpcURI, err := cmd.Flags().GetString(FlagNode)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -76,8 +75,8 @@ func (s *CometBFTServer[AppT, T]) StatusCommand() *cobra.Command {
},
}

cmd.Flags().StringP(flags.FlagNode, "n", "tcp://localhost:26657", "Node to connect to")
cmd.Flags().StringP(flags.FlagOutput, "o", "json", "Output format (text|json)")
cmd.Flags().StringP(FlagNode, "n", "tcp://localhost:26657", "Node to connect to")
cmd.Flags().StringP(FlagOutput, "o", "json", "Output format (text|json)")

return cmd
}
Expand Down Expand Up @@ -203,8 +202,8 @@ for. Each module documents its respective events under 'xx_events.md'.
}

query, _ := cmd.Flags().GetString(auth.FlagQuery)
page, _ := cmd.Flags().GetInt(flags.FlagPage)
limit, _ := cmd.Flags().GetInt(flags.FlagLimit)
page, _ := cmd.Flags().GetInt(FlagPage)
limit, _ := cmd.Flags().GetInt(FlagLimit)
orderBy, _ := cmd.Flags().GetString(auth.FlagOrderBy)

blocks, err := rpc.QueryBlocks(cmd.Context(), rpcclient, page, limit, query, orderBy)
Expand All @@ -221,9 +220,9 @@ for. Each module documents its respective events under 'xx_events.md'.
},
}

flags.AddQueryFlagsToCmd(cmd)
cmd.Flags().Int(flags.FlagPage, query.DefaultPage, "Query a specific page of paginated results")
cmd.Flags().Int(flags.FlagLimit, query.DefaultLimit, "Query number of transactions results per page returned")
AddQueryFlagsToCmd(cmd)
cmd.Flags().Int(FlagPage, query.DefaultPage, "Query a specific page of paginated results")
cmd.Flags().Int(FlagLimit, query.DefaultLimit, "Query number of transactions results per page returned")
cmd.Flags().String(auth.FlagQuery, "", "The blocks events query per CometBFT's query semantics")
cmd.Flags().String(auth.FlagOrderBy, "", "The ordering semantics (asc|dsc)")
_ = cmd.MarkFlagRequired(auth.FlagQuery)
Expand Down Expand Up @@ -312,7 +311,7 @@ $ %s query block --%s=%s <hash>
},
}

flags.AddQueryFlagsToCmd(cmd)
AddQueryFlagsToCmd(cmd)
cmd.Flags().String(auth.FlagType, auth.TypeHash, fmt.Sprintf("The type to be used when querying tx, can be one of \"%s\", \"%s\"", auth.TypeHeight, auth.TypeHash))

return cmd
Expand Down Expand Up @@ -364,7 +363,7 @@ func (s *CometBFTServer[AppT, T]) QueryBlockResultsCmd() *cobra.Command {
},
}

flags.AddQueryFlagsToCmd(cmd)
AddQueryFlagsToCmd(cmd)

return cmd
}
Expand Down Expand Up @@ -396,7 +395,7 @@ func (s *CometBFTServer[AppT, T]) BootstrapStateCmd() *cobra.Command {
return err
}
if height == 0 {
height, err = s.App.store.GetLatestVersion()
height, err = s.Consensus.store.GetLatestVersion()
if err != nil {
return err
}
Expand All @@ -414,7 +413,7 @@ func (s *CometBFTServer[AppT, T]) BootstrapStateCmd() *cobra.Command {

func printOutput(cmd *cobra.Command, out []byte) error {
// Get flags output
outFlag, err := cmd.Flags().GetString(flags.FlagOutput)
outFlag, err := cmd.Flags().GetString(FlagOutput)
if err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions server/v2/cometbft/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"cosmossdk.io/server/v2/cometbft/types"
)

// TODO REDO/VERIFY THIS

func GetConfigFromViper(v *viper.Viper) *cmtcfg.Config {
conf := cmtcfg.DefaultConfig()
err := v.Unmarshal(conf)
Expand Down
52 changes: 52 additions & 0 deletions server/v2/cometbft/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package cometbft

import "github.com/spf13/cobra"

const (
FlagQuery = "query"
FlagType = "type"
FlagOrderBy = "order_by"
)

const (
FlagWithComet = "with-comet"
FlagAddress = "address"
FlagTransport = "transport"
FlagTraceStore = "trace-store"
FlagCPUProfile = "cpu-profile"
FlagMinGasPrices = "minimum-gas-prices"
FlagQueryGasLimit = "query-gas-limit"
FlagHaltHeight = "halt-height"
FlagHaltTime = "halt-time"
FlagTrace = "trace"
)

const (
FlagChainID = "chain-id"
FlagNode = "node"
FlagGRPC = "grpc-addr"
FlagGRPCInsecure = "grpc-insecure"
FlagHeight = "height"
FlagPage = "page"
FlagLimit = "limit"
FlagOutput = "output"
)

// List of supported output formats
const (
OutputFormatJSON = "json"
OutputFormatText = "text"
)

// AddQueryFlagsToCmd adds common flags to a module query command.
func AddQueryFlagsToCmd(cmd *cobra.Command) {
cmd.Flags().String(FlagNode, "tcp://localhost:26657", "<host>:<port> to CometBFT RPC interface for this chain")
cmd.Flags().String(FlagGRPC, "", "the gRPC endpoint to use for this chain")
cmd.Flags().Bool(FlagGRPCInsecure, false, "allow gRPC over insecure channels, if not the server must use TLS")
cmd.Flags().Int64(FlagHeight, 0, "Use a specific height to query state at (this can error if the node is pruning state)")
cmd.Flags().StringP(FlagOutput, "o", OutputFormatText, "Output format (text|json)")

// some base commands does not require chainID e.g `simd testnet` while subcommands do
// hence the flag should not be required for those commands
_ = cmd.MarkFlagRequired(FlagChainID)
}
76 changes: 0 additions & 76 deletions server/v2/cometbft/flags/flags.go

This file was deleted.

32 changes: 32 additions & 0 deletions server/v2/cometbft/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cometbft

import (
"cosmossdk.io/core/transaction"
"cosmossdk.io/server/v2/cometbft/handlers"
"cosmossdk.io/server/v2/cometbft/mempool"
"cosmossdk.io/store/v2/snapshots"
)

// ServerOptions defines the options for the CometBFT server.
type ServerOptions[T transaction.Tx] struct {
Mempool mempool.Mempool[T]
PrepareProposalHandler handlers.PrepareHandler[T]
ProcessProposalHandler handlers.ProcessHandler[T]
VerifyVoteExtensionHandler handlers.VerifyVoteExtensionhandler
ExtendVoteHandler handlers.ExtendVoteHandler

SnapshotOptions snapshots.SnapshotOptions
}

// DefaultServerOptions returns the default server options.
// It defaults to a NoOpMempool and NoOp handlers.
func DefaultServerOptions[T transaction.Tx]() ServerOptions[T] {
return ServerOptions[T]{
Mempool: mempool.NoOpMempool[T]{},
PrepareProposalHandler: handlers.NoOpPrepareProposal[T](),
ProcessProposalHandler: handlers.NoOpProcessProposal[T](),
VerifyVoteExtensionHandler: handlers.NoOpVerifyVoteExtensionHandler(),
ExtendVoteHandler: handlers.NoOpExtendVote(),
SnapshotOptions: snapshots.NewSnapshotOptions(0, 0),
}
}
Loading

0 comments on commit afddef3

Please sign in to comment.