Skip to content

Commit

Permalink
WIP: add min-gas-prices in main server component
Browse files Browse the repository at this point in the history
  • Loading branch information
akhilkumarpilli committed Aug 7, 2024
1 parent 36e4aed commit bc8ad35
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 4 deletions.
24 changes: 24 additions & 0 deletions server/v2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
11 changes: 11 additions & 0 deletions server/v2/flags.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
41 changes: 40 additions & 1 deletion server/v2/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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](
Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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())
Expand Down
6 changes: 3 additions & 3 deletions simapp/v2/simdv2/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bc8ad35

Please sign in to comment.