diff --git a/server/v2/config.go b/server/v2/config.go index 7ce73fe88a9a..69aa25c3bdad 100644 --- a/server/v2/config.go +++ b/server/v2/config.go @@ -7,6 +7,30 @@ import ( "github.com/spf13/viper" ) +// ServerConfig defines configuration for the main server. +type ServerConfig struct { + MinGasPrices string `mapstructure:"minimum-gas-prices" toml:"minimum-gas-prices" comment:"minimum-gas-prices defines the price which a validator is willing to accept for processing a transaction. A transaction's fees must meet the minimum of any denomination specified in this config (e.g. 0.25token1;0.0001token2)."` +} + +// ValidateBasic returns an error if min-gas-prices field is empty in Config. Otherwise, it returns nil. +func (c ServerConfig) ValidateBasic() error { + if c.MinGasPrices == "" { + return fmt.Errorf("error in app.toml: set minimum-gas-prices in app.toml or flag or env variable") + } + + return nil +} + +// DefaultMainServerConfig returns the default config of main server component +func DefaultMainServerConfig() ServerConfig { + return ServerConfig{} +} + +// OverwriteDefaultConfig overwrites main server config with given config +func (s *Server[T]) OverwriteDefaultConfig(cfg ServerConfig) { + s.config = cfg +} + // ReadConfig returns a viper instance of the config file func ReadConfig(configPath string) (*viper.Viper, error) { v := viper.New() diff --git a/server/v2/flags.go b/server/v2/flags.go index a86154eb769c..6c253d419ab0 100644 --- a/server/v2/flags.go +++ b/server/v2/flags.go @@ -1,6 +1,17 @@ // Package serverv2 defines constants for server configuration flags and output formats. package serverv2 +import "fmt" + +// start flags are prefixed with the server name +// as the config in prefixed with the server name +// this allows viper to properly bind the flags +func prefix(f string) string { + return fmt.Sprintf("%s.%s", serverName, f) +} + +var FlagMinGasPrices = prefix("minimum-gas-prices") + const ( // FlagHome specifies the home directory flag. FlagHome = "home" diff --git a/server/v2/server.go b/server/v2/server.go index 6e1a4e7114db..2221358f5bae 100644 --- a/server/v2/server.go +++ b/server/v2/server.go @@ -56,11 +56,16 @@ type CLIConfig struct { Txs []*cobra.Command } +const ( + serverName = "server" +) + var _ ServerComponent[transaction.Tx] = (*Server[transaction.Tx])(nil) type Server[T transaction.Tx] struct { logger log.Logger components []ServerComponent[T] + config ServerConfig } func NewServer[T transaction.Tx]( @@ -74,11 +79,16 @@ func NewServer[T transaction.Tx]( } func (s *Server[T]) Name() string { - return "server" + return serverName } // Start starts all components concurrently. func (s *Server[T]) Start(ctx context.Context) error { + // validate main server config + if err := s.config.ValidateBasic(); err != nil { + return err + } + s.logger.Info("starting servers...") g, ctx := errgroup.WithContext(ctx) @@ -151,9 +161,19 @@ func (s *Server[T]) CLICommands() CLIConfig { return commands } +// Config returns config of the main server component +func (s *Server[T]) Config() ServerConfig { + return s.config +} + // Configs returns all configs of all server components. func (s *Server[T]) Configs() map[string]any { cfgs := make(map[string]any) + + // add main server component config + cfgs[s.Name()] = s.config + + // add other components' config for _, mod := range s.components { if configmod, ok := mod.(HasConfig); ok { cfg := configmod.Config() @@ -164,9 +184,22 @@ func (s *Server[T]) Configs() map[string]any { return cfgs } +func (s *Server[T]) StartCmdFlags() *pflag.FlagSet { + flags := pflag.NewFlagSet(s.Name(), pflag.ExitOnError) + flags.String(FlagMinGasPrices, "", "Minimum gas prices to accept for transactions; Any fee in a tx must meet this minimum (e.g. 0.01photino;0.0001stake)") + return flags +} + // Init initializes all server components with the provided application, configuration, and logger. // It returns an error if any component fails to initialize. func (s *Server[T]) Init(appI AppI[T], v *viper.Viper, logger log.Logger) error { + cfg := s.config + if v != nil { + if err := UnmarshalSubConfig(v, s.Name(), &cfg); err != nil { + return fmt.Errorf("failed to unmarshal config: %w", err) + } + } + var components []ServerComponent[T] for _, mod := range s.components { mod := mod @@ -177,6 +210,7 @@ func (s *Server[T]) Init(appI AppI[T], v *viper.Viper, logger log.Logger) error components = append(components, mod) } + s.config = cfg s.components = components return nil } @@ -217,6 +251,11 @@ func (s *Server[T]) WriteConfig(configPath string) error { // StartFlags returns all flags of all server components. func (s *Server[T]) StartFlags() []*pflag.FlagSet { flags := []*pflag.FlagSet{} + + // add main server component flags + flags = append(flags, s.StartCmdFlags()) + + // add other components' start cmd flags for _, mod := range s.components { if startmod, ok := mod.(HasStartFlags); ok { flags = append(flags, startmod.StartCmdFlags()) diff --git a/simapp/v2/simdv2/cmd/testnet.go b/simapp/v2/simdv2/cmd/testnet.go index 2a35b3c4b31b..823e4e1df7df 100644 --- a/simapp/v2/simdv2/cmd/testnet.go +++ b/simapp/v2/simdv2/cmd/testnet.go @@ -337,19 +337,19 @@ func initTestnetFiles[T transaction.Tx]( return err } - cometAppTomlCfg := cometbft.DefaultAppTomlConfig() - cometAppTomlCfg.MinGasPrices = args.minGasPrices + serverCfg := serverv2.DefaultMainServerConfig() + serverCfg.MinGasPrices = args.minGasPrices // Write server config cometServer := cometbft.New[T]( &genericTxDecoder[T]{clientCtx.TxConfig}, cometbft.ServerOptions[T]{}, cometbft.OverwriteDefaultConfigTomlConfig(nodeConfig), - cometbft.OverwriteDefaultAppTomlConfig(cometAppTomlCfg), ) grpcServer := grpc.New[T](grpc.OverwriteDefaultConfig(grpcConfig)) storeServer := store.New[T]() server := serverv2.NewServer(log.NewNopLogger(), cometServer, grpcServer, storeServer) + server.OverwriteDefaultConfig(serverCfg) err = server.WriteConfig(filepath.Join(nodeDir, "config")) if err != nil { return err