diff --git a/runtime/v2/app.go b/runtime/v2/app.go index 4f06127a9406..3f7f042e6a40 100644 --- a/runtime/v2/app.go +++ b/runtime/v2/app.go @@ -127,10 +127,6 @@ func (a *App) ExecuteGenesisTx(_ []byte) error { panic("App.ExecuteGenesisTx not supported in runtime/v2") } -func (a *App) SetAppVersion(context.Context, uint64) error { - return nil -} - -func (a *App) AppVersion(context.Context) (uint64, error) { - return 0, nil +func (a *App) GetAppManager() *appmanager.AppManager[transaction.Tx] { + return a.AppManager } diff --git a/scripts/simapp-v2-init.sh b/scripts/simapp-v2-init.sh index 922a40649ba3..6bdb51de7b57 100755 --- a/scripts/simapp-v2-init.sh +++ b/scripts/simapp-v2-init.sh @@ -13,13 +13,12 @@ CONFIG="${CONFIG:-$HOME/.simappv2/config}" cd "$SIMAPP_DIR" go build -o "$ROOT/build/simdv2" simdv2/main.go +if [ -d "$($SIMD config home)" ]; then rm -r $($SIMD config home); fi + $SIMD init simapp-v2-node --chain-id simapp-v2-chain cd "$CONFIG" -# to enable the api server -$SIMD config set app api.enable true - # to change the voting_period jq '.app_state.gov.voting_params.voting_period = "600s"' genesis.json > temp.json && mv temp.json genesis.json diff --git a/server/v2/api/grpc/server.go b/server/v2/api/grpc/server.go index 604a4f329fc8..9d3c95d92558 100644 --- a/server/v2/api/grpc/server.go +++ b/server/v2/api/grpc/server.go @@ -15,7 +15,9 @@ import ( _ "cosmossdk.io/api/amino" // Import amino.proto file for reflection appmanager "cosmossdk.io/core/app" + "cosmossdk.io/core/transaction" "cosmossdk.io/log" + serverv2 "cosmossdk.io/server/v2" "cosmossdk.io/server/v2/api/grpc/gogoreflection" ) @@ -23,9 +25,9 @@ const serverName = "grpc-server" type GRPCServer struct { logger log.Logger + config *Config grpcSrv *grpc.Server - config *Config } type GRPCService interface { @@ -33,9 +35,13 @@ type GRPCService interface { RegisterGRPCServer(gogogrpc.Server) } -// New returns a correctly configured and initialized gRPC server. +func New() GRPCServer { + return GRPCServer{} +} + +// Init returns a correctly configured and initialized gRPC server. // Note, the caller is responsible for starting the server. -func New(logger log.Logger, v *viper.Viper, interfaceRegistry appmanager.InterfaceRegistry, app GRPCService) (GRPCServer, error) { +func (g GRPCServer) Init(appI serverv2.AppI[transaction.Tx], v *viper.Viper, logger log.Logger) (serverv2.ServerComponent[transaction.Tx], error) { cfg := DefaultConfig() if v != nil { if err := v.Sub(serverName).Unmarshal(&cfg); err != nil { @@ -44,12 +50,12 @@ func New(logger log.Logger, v *viper.Viper, interfaceRegistry appmanager.Interfa } grpcSrv := grpc.NewServer( - grpc.ForceServerCodec(newProtoCodec(interfaceRegistry).GRPCCodec()), + grpc.ForceServerCodec(newProtoCodec(appI.InterfaceRegistry()).GRPCCodec()), grpc.MaxSendMsgSize(cfg.MaxSendMsgSize), grpc.MaxRecvMsgSize(cfg.MaxRecvMsgSize), ) - app.RegisterGRPCServer(grpcSrv) + // appI.RegisterGRPCServer(grpcSrv) // Reflection allows external clients to see what services and methods // the gRPC server exposes. diff --git a/server/v2/cometbft/config.go b/server/v2/cometbft/config.go index 40b987abb2b3..d6a546004f18 100644 --- a/server/v2/cometbft/config.go +++ b/server/v2/cometbft/config.go @@ -2,11 +2,24 @@ package cometbft import ( cmtcfg "github.com/cometbft/cometbft/config" + "github.com/spf13/viper" + serverv2 "cosmossdk.io/server/v2" "cosmossdk.io/server/v2/api/grpc" "cosmossdk.io/server/v2/cometbft/types" ) +func GetConfigFromViper(v *viper.Viper) *cmtcfg.Config { + conf := cmtcfg.DefaultConfig() + err := v.Unmarshal(conf) + rootDir := v.GetString(serverv2.FlagHome) + if err != nil { + return cmtcfg.DefaultConfig().SetRoot(rootDir) + } + + return conf.SetRoot(rootDir) +} + // Config is the configuration for the CometBFT application type Config struct { // app.toml config options diff --git a/server/v2/cometbft/go.mod b/server/v2/cometbft/go.mod index 8ff08bc18695..6e92622a21c8 100644 --- a/server/v2/cometbft/go.mod +++ b/server/v2/cometbft/go.mod @@ -7,6 +7,7 @@ replace ( cosmossdk.io/core => ../../../core cosmossdk.io/core/testing => ../../../core/testing cosmossdk.io/depinject => ../../../depinject + cosmossdk.io/log => ../../../log cosmossdk.io/server/v2 => ../ cosmossdk.io/server/v2/appmanager => ../appmanager cosmossdk.io/store => ../../../store @@ -38,6 +39,7 @@ require ( github.com/grpc-ecosystem/grpc-gateway v1.16.0 github.com/spf13/cobra v1.8.1 github.com/spf13/pflag v1.0.5 + github.com/spf13/viper v1.19.0 google.golang.org/genproto/googleapis/api v0.0.0-20240318140521-94a12d6c2237 google.golang.org/grpc v1.64.0 google.golang.org/protobuf v1.34.2 @@ -156,7 +158,6 @@ require ( github.com/sourcegraph/conc v0.3.0 // indirect github.com/spf13/afero v1.11.0 // indirect github.com/spf13/cast v1.6.0 // indirect - github.com/spf13/viper v1.19.0 // indirect github.com/stretchr/testify v1.9.0 // indirect github.com/subosito/gotenv v1.6.0 // indirect github.com/supranational/blst v0.3.11 // indirect diff --git a/server/v2/cometbft/go.sum b/server/v2/cometbft/go.sum index e72f36caa0ef..e0b31666fe83 100644 --- a/server/v2/cometbft/go.sum +++ b/server/v2/cometbft/go.sum @@ -8,8 +8,6 @@ cosmossdk.io/collections v0.4.0 h1:PFmwj2W8szgpD5nOd8GWH6AbYNi1f2J6akWXJ7P5t9s= cosmossdk.io/collections v0.4.0/go.mod h1:oa5lUING2dP+gdDquow+QjlF45eL1t4TJDypgGd+tv0= cosmossdk.io/errors v1.0.1 h1:bzu+Kcr0kS/1DuPBtUFdWjzLqyUuCiyHjyJB6srBV/0= cosmossdk.io/errors v1.0.1/go.mod h1:MeelVSZThMi4bEakzhhhE/CKqVv3nOJDA25bIqRDu/U= -cosmossdk.io/log v1.3.1 h1:UZx8nWIkfbbNEWusZqzAx3ZGvu54TZacWib3EzUYmGI= -cosmossdk.io/log v1.3.1/go.mod h1:2/dIomt8mKdk6vl3OWJcPk2be3pGOS8OQaLUM/3/tCM= cosmossdk.io/math v1.3.0 h1:RC+jryuKeytIiictDslBP9i1fhkVm6ZDmZEoNP316zE= cosmossdk.io/math v1.3.0/go.mod h1:vnRTxewy+M7BtXBNFybkuhSH4WfedVAAnERHgVFhp3k= cosmossdk.io/x/accounts/defaults/lockup v0.0.0-20240417181816-5e7aae0db1f5 h1:eb0kcGyaYHSS0do7+MIWg7UKlskSH01biRNENbm/zDA= diff --git a/server/v2/cometbft/server.go b/server/v2/cometbft/server.go index dc7346b034f8..eb9ca0d41ed9 100644 --- a/server/v2/cometbft/server.go +++ b/server/v2/cometbft/server.go @@ -5,6 +5,7 @@ import ( "crypto/sha256" "encoding/json" "fmt" + "path/filepath" abciserver "github.com/cometbft/cometbft/abci/server" cmtcmd "github.com/cometbft/cometbft/cmd/cometbft/commands" @@ -15,7 +16,9 @@ import ( "github.com/cometbft/cometbft/proxy" "github.com/spf13/cobra" "github.com/spf13/pflag" + "github.com/spf13/viper" + corectx "cosmossdk.io/core/context" "cosmossdk.io/core/log" "cosmossdk.io/core/transaction" serverv2 "cosmossdk.io/server/v2" @@ -42,15 +45,14 @@ const ( FlagTrace = "trace" ) -var _ serverv2.ServerModule = (*CometBFTServer[transaction.Tx])(nil) +var _ serverv2.ServerComponent[transaction.Tx] = (*CometBFTServer[transaction.Tx])(nil) type CometBFTServer[T transaction.Tx] struct { Node *node.Node App *Consensus[T] logger log.Logger - config Config - cleanupFn func() + config Config } // App is an interface that represents an application in the CometBFT server. @@ -61,20 +63,25 @@ type App[T transaction.Tx] interface { GetStore() types.Store } -func NewCometBFTServer[T transaction.Tx]( - app *appmanager.AppManager[T], - store types.Store, - logger log.Logger, - cfg Config, - txCodec transaction.Codec[T], -) *CometBFTServer[T] { +func New[T transaction.Tx](txCodec transaction.Codec[T]) *CometBFTServer[T] { + consensus := &Consensus[T]{txCodec: txCodec} + return &CometBFTServer[T]{ + App: consensus, + } +} + +func (s *CometBFTServer[T]) Init(appI serverv2.AppI[T], v *viper.Viper, logger log.Logger) (serverv2.ServerComponent[T], error) { + store := appI.GetStore().(types.Store) + + cfg := Config{CmtConfig: GetConfigFromViper(v), ConsensusAuthority: appI.GetConsensusAuthority()} logger = logger.With("module", "cometbft-server") // create noop mempool mempool := mempool.NoOpMempool[T]{} // create consensus - consensus := NewConsensus[T](app, mempool, store, cfg, txCodec, logger) + // txCodec should be in server from New() + consensus := NewConsensus[T](appI.GetAppManager(), mempool, store, cfg, s.App.txCodec, logger) consensus.SetPrepareProposalHandler(handlers.NoOpPrepareProposal[T]()) consensus.SetProcessProposalHandler(handlers.NoOpProcessProposal[T]()) @@ -93,11 +100,15 @@ func NewCometBFTServer[T transaction.Tx]( sm := snapshots.NewManager(snapshotStore, snapshots.SnapshotOptions{}, sc, ss, nil, logger) // TODO: set options somehow consensus.SetSnapshotManager(sm) + s.config = cfg + s.App = consensus + s.logger = logger + return &CometBFTServer[T]{ logger: logger, App: consensus, config: cfg, - } + }, nil } func (s *CometBFTServer[T]) Name() string { @@ -105,6 +116,9 @@ func (s *CometBFTServer[T]) Name() string { } func (s *CometBFTServer[T]) Start(ctx context.Context) error { + viper := ctx.Value(corectx.ViperContextKey{}).(*viper.Viper) + cometConfig := GetConfigFromViper(viper) + wrappedLogger := cometlog.CometLoggerWrapper{Logger: s.logger} if s.config.Standalone { svr, err := abciserver.NewServer(s.config.Addr, s.config.Transport, s.App) @@ -117,40 +131,34 @@ func (s *CometBFTServer[T]) Start(ctx context.Context) error { return svr.Start() } - nodeKey, err := p2p.LoadOrGenNodeKey(s.config.CmtConfig.NodeKeyFile()) + nodeKey, err := p2p.LoadOrGenNodeKey(cometConfig.NodeKeyFile()) if err != nil { return err } s.Node, err = node.NewNode( ctx, - s.config.CmtConfig, - pvm.LoadOrGenFilePV(s.config.CmtConfig.PrivValidatorKeyFile(), s.config.CmtConfig.PrivValidatorStateFile()), + cometConfig, + pvm.LoadOrGenFilePV(cometConfig.PrivValidatorKeyFile(), cometConfig.PrivValidatorStateFile()), nodeKey, proxy.NewLocalClientCreator(s.App), - getGenDocProvider(s.config.CmtConfig), + getGenDocProvider(cometConfig), cmtcfg.DefaultDBProvider, - node.DefaultMetricsProvider(s.config.CmtConfig.Instrumentation), + node.DefaultMetricsProvider(cometConfig.Instrumentation), wrappedLogger, ) if err != nil { return err } - s.cleanupFn = func() { - if s.Node != nil && s.Node.IsRunning() { - _ = s.Node.Stop() - } - } - return s.Node.Start() } -func (s *CometBFTServer[T]) Stop(_ context.Context) error { - defer s.cleanupFn() - if s.Node != nil { +func (s *CometBFTServer[T]) Stop(context.Context) error { + if s.Node != nil && s.Node.IsRunning() { return s.Node.Stop() } + return nil } @@ -223,3 +231,9 @@ func (s *CometBFTServer[T]) CLICommands() serverv2.CLIConfig { }, } } + +func (s *CometBFTServer[T]) WriteDefaultConfigAt(configPath string) error { + cometConfig := cmtcfg.DefaultConfig() + cmtcfg.WriteConfigFile(filepath.Join(configPath, "config.toml"), cometConfig) + return nil +} diff --git a/server/v2/commands.go b/server/v2/commands.go index f87911ba940f..82d7cb869e0b 100644 --- a/server/v2/commands.go +++ b/server/v2/commands.go @@ -11,27 +11,38 @@ import ( "github.com/spf13/cobra" + "cosmossdk.io/core/transaction" "cosmossdk.io/log" ) -func Commands(logger log.Logger, homePath string, modules ...ServerModule) (CLIConfig, error) { - if len(modules) == 0 { - // TODO figure if we should define default modules - // and if so it should be done here to avoid unnecessary dependencies - return CLIConfig{}, errors.New("no modules provided") +func Commands(rootCmd *cobra.Command, newApp AppCreator[transaction.Tx], logger log.Logger, components ...ServerComponent[transaction.Tx]) (CLIConfig, error) { + if len(components) == 0 { + return CLIConfig{}, errors.New("no components provided") } - v, err := ReadConfig(filepath.Join(homePath, "config")) - if err != nil { - return CLIConfig{}, fmt.Errorf("failed to read config: %w", err) - } + server := NewServer(logger, components...) + flags := server.StartFlags() - server := NewServer(logger, modules...) startCmd := &cobra.Command{ Use: "start", Short: "Run the application", RunE: func(cmd *cobra.Command, args []string) error { - if err := v.BindPFlags(cmd.Flags()); err != nil { // the server modules are already instantiated here, so binding the flags is useless. + v := GetViperFromCmd(cmd) + l := GetLoggerFromCmd(cmd) + + for _, startFlags := range flags { + if err := v.BindPFlags(startFlags); err != nil { + return err + } + } + + if err := v.BindPFlags(cmd.Flags()); err != nil { + return err + } + + app := newApp(l, v) + + if _, err := server.Init(app, v, l); err != nil { return err } @@ -65,12 +76,58 @@ func Commands(logger log.Logger, homePath string, modules ...ServerModule) (CLIC return cmds, nil } -func AddCommands(rootCmd *cobra.Command, logger log.Logger, homePath string, modules ...ServerModule) error { - cmds, err := Commands(logger, homePath, modules...) +func AddCommands(rootCmd *cobra.Command, newApp AppCreator[transaction.Tx], logger log.Logger, components ...ServerComponent[transaction.Tx]) error { + cmds, err := Commands(rootCmd, newApp, logger, components...) if err != nil { return err } + server := NewServer(logger, components...) + originalPersistentPreRunE := rootCmd.PersistentPreRunE + rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error { + home, err := cmd.Flags().GetString(FlagHome) + if err != nil { + return err + } + + err = configHandle(server, home, cmd) + if err != nil { + return err + } + + if rootCmd.PersistentPreRun != nil { + rootCmd.PersistentPreRun(cmd, args) + return nil + } + + return originalPersistentPreRunE(cmd, args) + } + rootCmd.AddCommand(cmds.Commands...) return nil } + +// configHandle writes the default config to the home directory if it does not exist and sets the server context +func configHandle(s *Server, home string, cmd *cobra.Command) error { + if _, err := os.Stat(filepath.Join(home, "config")); os.IsNotExist(err) { + if err = s.WriteConfig(filepath.Join(home, "config")); err != nil { + return err + } + } + + viper, err := ReadConfig(filepath.Join(home, "config")) + if err != nil { + return err + } + viper.Set(FlagHome, home) + if err := viper.BindPFlags(cmd.Flags()); err != nil { + return err + } + + log, err := NewLogger(viper, cmd.OutOrStdout()) + if err != nil { + return err + } + + return SetCmdServerContext(cmd, viper, log) +} diff --git a/server/v2/flags.go b/server/v2/flags.go index e1d70195143f..6ef849a31dfa 100644 --- a/server/v2/flags.go +++ b/server/v2/flags.go @@ -1,6 +1,9 @@ package serverv2 const ( + // Home flags + FlagHome = "home" + // Logging flags FlagLogLevel = "log_level" FlagLogFormat = "log_format" diff --git a/server/v2/go.mod b/server/v2/go.mod index 5e2ace131236..37d36c34aaba 100644 --- a/server/v2/go.mod +++ b/server/v2/go.mod @@ -6,6 +6,7 @@ replace ( cosmossdk.io/api => ../../api cosmossdk.io/core => ../../core cosmossdk.io/depinject => ../../depinject + cosmossdk.io/log => ../../log cosmossdk.io/server/v2 => . cosmossdk.io/server/v2/appmanager => ./appmanager cosmossdk.io/server/v2/stf => ./stf @@ -16,6 +17,7 @@ require ( cosmossdk.io/api v0.7.5 cosmossdk.io/core v0.12.1-0.20231114100755-569e3ff6a0d7 cosmossdk.io/log v1.3.1 + cosmossdk.io/server/v2/appmanager v0.0.0-00010101000000-000000000000 github.com/cosmos/cosmos-proto v1.0.0-beta.5 github.com/cosmos/gogogateway v1.2.0 github.com/cosmos/gogoproto v1.5.0 @@ -30,6 +32,7 @@ require ( github.com/prometheus/common v0.54.0 github.com/rs/zerolog v1.33.0 github.com/spf13/cobra v1.8.1 + github.com/spf13/pflag v1.0.5 github.com/spf13/viper v1.19.0 github.com/stretchr/testify v1.9.0 golang.org/x/sync v0.7.0 @@ -70,7 +73,6 @@ require ( github.com/sourcegraph/conc v0.3.0 // indirect github.com/spf13/afero v1.11.0 // indirect github.com/spf13/cast v1.6.0 // indirect - github.com/spf13/pflag v1.0.5 // indirect github.com/subosito/gotenv v1.6.0 // indirect go.uber.org/multierr v1.11.0 // indirect golang.org/x/exp v0.0.0-20240531132922-fd00a4e0eefc // indirect diff --git a/server/v2/go.sum b/server/v2/go.sum index c40e5066a35c..9834b5ac704f 100644 --- a/server/v2/go.sum +++ b/server/v2/go.sum @@ -1,7 +1,5 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= -cosmossdk.io/log v1.3.1 h1:UZx8nWIkfbbNEWusZqzAx3ZGvu54TZacWib3EzUYmGI= -cosmossdk.io/log v1.3.1/go.mod h1:2/dIomt8mKdk6vl3OWJcPk2be3pGOS8OQaLUM/3/tCM= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/DataDog/datadog-go v3.2.0+incompatible h1:qSG2N4FghB1He/r2mFrWKCaL7dXCilEuNEeAn20fdD4= github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ= diff --git a/server/v2/server.go b/server/v2/server.go index c398f154717f..bfdccaf37fd0 100644 --- a/server/v2/server.go +++ b/server/v2/server.go @@ -4,20 +4,24 @@ import ( "context" "fmt" "os" + "path/filepath" "github.com/pelletier/go-toml/v2" + "github.com/spf13/pflag" "github.com/spf13/viper" "golang.org/x/sync/errgroup" + "cosmossdk.io/core/transaction" "cosmossdk.io/log" ) -// ServerModule is a server module that can be started and stopped. -type ServerModule interface { +// ServerComponent is a server module that can be started and stopped. +type ServerComponent[T transaction.Tx] interface { Name() string Start(context.Context) error Stop(context.Context) error + Init(AppI[T], *viper.Viper, log.Logger) (ServerComponent[T], error) } // HasCLICommands is a server module that has CLI commands. @@ -30,30 +34,43 @@ type HasConfig interface { Config() any } -var _ ServerModule = (*Server)(nil) +// HasStartFlags is a server module that has start flags. +type HasStartFlags interface { + StartCmdFlags() *pflag.FlagSet +} + +var _ ServerComponent[transaction.Tx] = (*Server)(nil) // Configs returns a viper instance of the config file func ReadConfig(configPath string) (*viper.Viper, error) { v := viper.New() - v.SetConfigFile(configPath) + v.SetConfigType("toml") + v.SetConfigName("config") + v.AddConfigPath(configPath) if err := v.ReadInConfig(); err != nil { return nil, fmt.Errorf("failed to read config: %s: %w", configPath, err) } + + v.SetConfigName("app") + if err := v.MergeInConfig(); err != nil { + return nil, fmt.Errorf("failed to merge configuration: %w", err) + } + v.WatchConfig() return v, nil } type Server struct { - logger log.Logger - modules []ServerModule + logger log.Logger + components []ServerComponent[transaction.Tx] } -func NewServer(logger log.Logger, modules ...ServerModule) *Server { +func NewServer(logger log.Logger, components ...ServerComponent[transaction.Tx]) *Server { return &Server{ - logger: logger, - modules: modules, + logger: logger, + components: components, } } @@ -61,12 +78,12 @@ func (s *Server) Name() string { return "server" } -// Start starts all modules concurrently. +// Start starts all components concurrently. func (s *Server) Start(ctx context.Context) error { s.logger.Info("starting servers...") g, ctx := errgroup.WithContext(ctx) - for _, mod := range s.modules { + for _, mod := range s.components { mod := mod g.Go(func() error { return mod.Start(ctx) @@ -85,12 +102,12 @@ func (s *Server) Start(ctx context.Context) error { return nil } -// Stop stops all modules concurrently. +// Stop stops all components concurrently. func (s *Server) Stop(ctx context.Context) error { s.logger.Info("stopping servers...") g, ctx := errgroup.WithContext(ctx) - for _, mod := range s.modules { + for _, mod := range s.components { mod := mod g.Go(func() error { return mod.Stop(ctx) @@ -100,10 +117,10 @@ func (s *Server) Stop(ctx context.Context) error { return g.Wait() } -// CLICommands returns all CLI commands of all modules. +// CLICommands returns all CLI commands of all components. func (s *Server) CLICommands() CLIConfig { commands := CLIConfig{} - for _, mod := range s.modules { + for _, mod := range s.components { if climod, ok := mod.(HasCLICommands); ok { commands.Commands = append(commands.Commands, climod.CLICommands().Commands...) commands.Queries = append(commands.Queries, climod.CLICommands().Queries...) @@ -114,10 +131,10 @@ func (s *Server) CLICommands() CLIConfig { return commands } -// Configs returns all configs of all server modules. +// Configs returns all configs of all server components. func (s *Server) Configs() map[string]any { cfgs := make(map[string]any) - for _, mod := range s.modules { + for _, mod := range s.components { if configmod, ok := mod.(HasConfig); ok { cfg := configmod.Config() cfgs[mod.Name()] = cfg @@ -127,18 +144,60 @@ func (s *Server) Configs() map[string]any { return cfgs } +// Configs returns all configs of all server components. +func (s *Server) Init(appI AppI[transaction.Tx], v *viper.Viper, logger log.Logger) (ServerComponent[transaction.Tx], error) { + var components []ServerComponent[transaction.Tx] + for _, mod := range s.components { + mod := mod + module, err := mod.Init(appI, v, logger) + if err != nil { + return nil, err + } + components = append(components, module) + } + s.components = components + + return s, nil +} + // WriteConfig writes the config to the given path. // Note: it does not use viper.WriteConfigAs because we do not want to store flag values in the config. func (s *Server) WriteConfig(configPath string) error { cfgs := s.Configs() b, err := toml.Marshal(cfgs) if err != nil { - return fmt.Errorf("failed to marshal config: %w", err) + return err } - if err := os.WriteFile(configPath, b, 0o600); err != nil { + if _, err := os.Stat(configPath); os.IsNotExist(err) { + if err := os.MkdirAll(configPath, os.ModePerm); err != nil { + return err + } + } + + if err := os.WriteFile(filepath.Join(configPath, "app.toml"), b, 0o600); err != nil { return fmt.Errorf("failed to write config: %w", err) } + for _, component := range s.components { + if mod, ok := component.(interface{ WriteDefaultConfigAt(string) error }); ok { + if err := mod.WriteDefaultConfigAt(configPath); err != nil { + return err + } + } + } + return nil } + +// Flags returns all flags of all server components. +func (s *Server) StartFlags() []*pflag.FlagSet { + flags := []*pflag.FlagSet{} + for _, mod := range s.components { + if startmod, ok := mod.(HasStartFlags); ok { + flags = append(flags, startmod.StartCmdFlags()) + } + } + + return flags +} diff --git a/server/v2/server_mock_test.go b/server/v2/server_mock_test.go index 2158549eed0c..f805c97a1663 100644 --- a/server/v2/server_mock_test.go +++ b/server/v2/server_mock_test.go @@ -4,6 +4,12 @@ import ( "context" "fmt" "math/rand" + + "github.com/spf13/viper" + + "cosmossdk.io/core/transaction" + "cosmossdk.io/log" + serverv2 "cosmossdk.io/server/v2" ) type mockServerConfig struct { @@ -48,3 +54,7 @@ func (s *mockServer) Stop(ctx context.Context) error { func (s *mockServer) Config() any { return MockServerDefaultConfig() } + +func (s *mockServer) Init(appI serverv2.AppI[transaction.Tx], v *viper.Viper, logger log.Logger) (serverv2.ServerComponent[transaction.Tx], error) { + return nil, nil +} diff --git a/server/v2/server_test.go b/server/v2/server_test.go index 7ccdad83a95a..2693a4674aed 100644 --- a/server/v2/server_test.go +++ b/server/v2/server_test.go @@ -7,21 +7,17 @@ import ( "testing" "time" - gogogrpc "github.com/cosmos/gogoproto/grpc" gogoproto "github.com/cosmos/gogoproto/proto" "github.com/spf13/viper" + "github.com/stretchr/testify/require" + coreapp "cosmossdk.io/core/app" + "cosmossdk.io/core/transaction" "cosmossdk.io/log" serverv2 "cosmossdk.io/server/v2" grpc "cosmossdk.io/server/v2/api/grpc" ) -type mockGRPCService struct { - grpc.GRPCService -} - -func (m *mockGRPCService) RegisterGRPCServer(gogogrpc.Server) {} - type mockInterfaceRegistry struct{} func (*mockInterfaceRegistry) Resolve(typeUrl string) (gogoproto.Message, error) { @@ -33,6 +29,14 @@ func (*mockInterfaceRegistry) ListImplementations(ifaceTypeURL string) []string } func (*mockInterfaceRegistry) ListAllInterfaces() []string { panic("not implemented") } +type mockApp[T transaction.Tx] struct { + serverv2.AppI[T] +} + +func (*mockApp[T]) InterfaceRegistry() coreapp.InterfaceRegistry { + return &mockInterfaceRegistry{} +} + // TODO split this test into multiple tests // test read config // test write config @@ -46,7 +50,7 @@ func TestServer(t *testing.T) { t.Log(err) t.Fail() } - configPath := filepath.Join(currentDir, "testdata", "app.toml") + configPath := filepath.Join(currentDir, "testdata") v, err := serverv2.ReadConfig(configPath) if err != nil { @@ -54,7 +58,7 @@ func TestServer(t *testing.T) { } logger := log.NewLogger(os.Stdout) - grpcServer, err := grpc.New(logger, v, &mockInterfaceRegistry{}, &mockGRPCService{}) + grpcServer, err := grpc.New().Init(&mockApp[transaction.Tx]{}, v, logger) if err != nil { t.Log(err) t.Fail() @@ -114,3 +118,19 @@ func TestServer(t *testing.T) { t.Fail() } } + +func TestReadConfig(t *testing.T) { + currentDir, err := os.Getwd() + if err != nil { + t.Log(err) + t.Fail() + } + configPath := filepath.Join(currentDir, "testdata") + + v, err := serverv2.ReadConfig(configPath) + require.NoError(t, err) + + grpcConfig := grpc.DefaultConfig() + err = v.Sub("grpc-server").Unmarshal(&grpcConfig) + require.NoError(t, err) +} diff --git a/server/v2/testdata/app.toml b/server/v2/testdata/app.toml index 412ba85fed92..512f0ce02033 100644 --- a/server/v2/testdata/app.toml +++ b/server/v2/testdata/app.toml @@ -1,6 +1,6 @@ [grpc-server] # Enable defines if the gRPC server should be enabled. -enable = false +enable = true # Address defines the gRPC server address to bind to. address = 'localhost:9090' # MaxRecvMsgSize defines the max message size in bytes the server can receive. diff --git a/server/v2/testdata/config.toml b/server/v2/testdata/config.toml new file mode 100644 index 000000000000..d42fa7f98d3c --- /dev/null +++ b/server/v2/testdata/config.toml @@ -0,0 +1,482 @@ +# This is a TOML config file. +# For more information, see https://github.com/toml-lang/toml + +# NOTE: Any path below can be absolute (e.g. "/var/myawesomeapp/data") or +# relative to the home directory (e.g. "data"). The home directory is +# "$HOME/.cometbft" by default, but could be changed via $CMTHOME env variable +# or --home cmd flag. + +# The version of the CometBFT binary that created or +# last modified the config file. Do not modify this. +version = "0.38.7" + +####################################################################### +### Main Base Config Options ### +####################################################################### + +# TCP or UNIX socket address of the ABCI application, +# or the name of an ABCI application compiled in with the CometBFT binary +proxy_app = "tcp://127.0.0.1:26658" + +# A custom human readable name for this node +moniker = "aurn-node" + +# Database backend: goleveldb | cleveldb | boltdb | rocksdb | badgerdb +# * goleveldb (github.com/syndtr/goleveldb - most popular implementation) +# - pure go +# - stable +# * cleveldb (uses levigo wrapper) +# - fast +# - requires gcc +# - use cleveldb build tag (go build -tags cleveldb) +# * boltdb (uses etcd's fork of bolt - github.com/etcd-io/bbolt) +# - EXPERIMENTAL +# - may be faster is some use-cases (random reads - indexer) +# - use boltdb build tag (go build -tags boltdb) +# * rocksdb (uses github.com/tecbot/gorocksdb) +# - EXPERIMENTAL +# - requires gcc +# - use rocksdb build tag (go build -tags rocksdb) +# * badgerdb (uses github.com/dgraph-io/badger) +# - EXPERIMENTAL +# - use badgerdb build tag (go build -tags badgerdb) +db_backend = "goleveldb" + +# Database directory +db_dir = "data" + +# Output level for logging, including package level options +log_level = "info" + +# Output format: 'plain' (colored text) or 'json' +log_format = "plain" + +##### additional base config options ##### + +# Path to the JSON file containing the initial validator set and other meta data +genesis_file = "config/genesis.json" + +# Path to the JSON file containing the private key to use as a validator in the consensus protocol +priv_validator_key_file = "config/priv_validator_key.json" + +# Path to the JSON file containing the last sign state of a validator +priv_validator_state_file = "data/priv_validator_state.json" + +# TCP or UNIX socket address for CometBFT to listen on for +# connections from an external PrivValidator process +priv_validator_laddr = "" + +# Path to the JSON file containing the private key to use for node authentication in the p2p protocol +node_key_file = "config/node_key.json" + +# Mechanism to connect to the ABCI application: socket | grpc +abci = "socket" + +# If true, query the ABCI app on connecting to a new peer +# so the app can decide if we should keep the connection or not +filter_peers = false + + +####################################################################### +### Advanced Configuration Options ### +####################################################################### + +####################################################### +### RPC Server Configuration Options ### +####################################################### +[rpc] + +# TCP or UNIX socket address for the RPC server to listen on +laddr = "tcp://127.0.0.1:26657" + +# A list of origins a cross-domain request can be executed from +# Default value '[]' disables cors support +# Use '["*"]' to allow any origin +cors_allowed_origins = [] + +# A list of methods the client is allowed to use with cross-domain requests +cors_allowed_methods = ["HEAD", "GET", "POST", ] + +# A list of non simple headers the client is allowed to use with cross-domain requests +cors_allowed_headers = ["Origin", "Accept", "Content-Type", "X-Requested-With", "X-Server-Time", ] + +# TCP or UNIX socket address for the gRPC server to listen on +# NOTE: This server only supports /broadcast_tx_commit +grpc_laddr = "" + +# Maximum number of simultaneous connections. +# Does not include RPC (HTTP&WebSocket) connections. See max_open_connections +# If you want to accept a larger number than the default, make sure +# you increase your OS limits. +# 0 - unlimited. +# Should be < {ulimit -Sn} - {MaxNumInboundPeers} - {MaxNumOutboundPeers} - {N of wal, db and other open files} +# 1024 - 40 - 10 - 50 = 924 = ~900 +grpc_max_open_connections = 900 + +# Activate unsafe RPC commands like /dial_seeds and /unsafe_flush_mempool +unsafe = false + +# Maximum number of simultaneous connections (including WebSocket). +# Does not include gRPC connections. See grpc_max_open_connections +# If you want to accept a larger number than the default, make sure +# you increase your OS limits. +# 0 - unlimited. +# Should be < {ulimit -Sn} - {MaxNumInboundPeers} - {MaxNumOutboundPeers} - {N of wal, db and other open files} +# 1024 - 40 - 10 - 50 = 924 = ~900 +max_open_connections = 900 + +# Maximum number of unique clientIDs that can /subscribe +# If you're using /broadcast_tx_commit, set to the estimated maximum number +# of broadcast_tx_commit calls per block. +max_subscription_clients = 100 + +# Maximum number of unique queries a given client can /subscribe to +# If you're using GRPC (or Local RPC client) and /broadcast_tx_commit, set to +# the estimated # maximum number of broadcast_tx_commit calls per block. +max_subscriptions_per_client = 5 + +# Experimental parameter to specify the maximum number of events a node will +# buffer, per subscription, before returning an error and closing the +# subscription. Must be set to at least 100, but higher values will accommodate +# higher event throughput rates (and will use more memory). +experimental_subscription_buffer_size = 200 + +# Experimental parameter to specify the maximum number of RPC responses that +# can be buffered per WebSocket client. If clients cannot read from the +# WebSocket endpoint fast enough, they will be disconnected, so increasing this +# parameter may reduce the chances of them being disconnected (but will cause +# the node to use more memory). +# +# Must be at least the same as "experimental_subscription_buffer_size", +# otherwise connections could be dropped unnecessarily. This value should +# ideally be somewhat higher than "experimental_subscription_buffer_size" to +# accommodate non-subscription-related RPC responses. +experimental_websocket_write_buffer_size = 200 + +# If a WebSocket client cannot read fast enough, at present we may +# silently drop events instead of generating an error or disconnecting the +# client. +# +# Enabling this experimental parameter will cause the WebSocket connection to +# be closed instead if it cannot read fast enough, allowing for greater +# predictability in subscription behavior. +experimental_close_on_slow_client = false + +# How long to wait for a tx to be committed during /broadcast_tx_commit. +# WARNING: Using a value larger than 10s will result in increasing the +# global HTTP write timeout, which applies to all connections and endpoints. +# See https://github.com/tendermint/tendermint/issues/3435 +timeout_broadcast_tx_commit = "10s" + +# Maximum size of request body, in bytes +max_body_bytes = 1000000 + +# Maximum size of request header, in bytes +max_header_bytes = 1048576 + +# The path to a file containing certificate that is used to create the HTTPS server. +# Might be either absolute path or path related to CometBFT's config directory. +# If the certificate is signed by a certificate authority, +# the certFile should be the concatenation of the server's certificate, any intermediates, +# and the CA's certificate. +# NOTE: both tls_cert_file and tls_key_file must be present for CometBFT to create HTTPS server. +# Otherwise, HTTP server is run. +tls_cert_file = "" + +# The path to a file containing matching private key that is used to create the HTTPS server. +# Might be either absolute path or path related to CometBFT's config directory. +# NOTE: both tls-cert-file and tls-key-file must be present for CometBFT to create HTTPS server. +# Otherwise, HTTP server is run. +tls_key_file = "" + +# pprof listen address (https://golang.org/pkg/net/http/pprof) +pprof_laddr = "" + +####################################################### +### P2P Configuration Options ### +####################################################### +[p2p] + +# Address to listen for incoming connections +laddr = "tcp://0.0.0.0:26656" + +# Address to advertise to peers for them to dial. If empty, will use the same +# port as the laddr, and will introspect on the listener to figure out the +# address. IP and port are required. Example: 159.89.10.97:26656 +external_address = "" + +# Comma separated list of seed nodes to connect to +seeds = "" + +# Comma separated list of nodes to keep persistent connections to +persistent_peers = "" + +# Path to address book +addr_book_file = "config/addrbook.json" + +# Set true for strict address routability rules +# Set false for private or local networks +addr_book_strict = true + +# Maximum number of inbound peers +max_num_inbound_peers = 40 + +# Maximum number of outbound peers to connect to, excluding persistent peers +max_num_outbound_peers = 10 + +# List of node IDs, to which a connection will be (re)established ignoring any existing limits +unconditional_peer_ids = "" + +# Maximum pause when redialing a persistent peer (if zero, exponential backoff is used) +persistent_peers_max_dial_period = "0s" + +# Time to wait before flushing messages out on the connection +flush_throttle_timeout = "100ms" + +# Maximum size of a message packet payload, in bytes +max_packet_msg_payload_size = 1024 + +# Rate at which packets can be sent, in bytes/second +send_rate = 5120000 + +# Rate at which packets can be received, in bytes/second +recv_rate = 5120000 + +# Set true to enable the peer-exchange reactor +pex = true + +# Seed mode, in which node constantly crawls the network and looks for +# peers. If another node asks it for addresses, it responds and disconnects. +# +# Does not work if the peer-exchange reactor is disabled. +seed_mode = false + +# Comma separated list of peer IDs to keep private (will not be gossiped to other peers) +private_peer_ids = "" + +# Toggle to disable guard against peers connecting from the same ip. +allow_duplicate_ip = false + +# Peer connection configuration. +handshake_timeout = "20s" +dial_timeout = "3s" + +####################################################### +### Mempool Configuration Option ### +####################################################### +[mempool] + +# The type of mempool for this node to use. +# +# Possible types: +# - "flood" : concurrent linked list mempool with flooding gossip protocol +# (default) +# - "nop" : nop-mempool (short for no operation; the ABCI app is responsible +# for storing, disseminating and proposing txs). "create_empty_blocks=false" is +# not supported. +type = "flood" + +# Recheck (default: true) defines whether CometBFT should recheck the +# validity for all remaining transaction in the mempool after a block. +# Since a block affects the application state, some transactions in the +# mempool may become invalid. If this does not apply to your application, +# you can disable rechecking. +recheck = true + +# Broadcast (default: true) defines whether the mempool should relay +# transactions to other peers. Setting this to false will stop the mempool +# from relaying transactions to other peers until they are included in a +# block. In other words, if Broadcast is disabled, only the peer you send +# the tx to will see it until it is included in a block. +broadcast = true + +# WalPath (default: "") configures the location of the Write Ahead Log +# (WAL) for the mempool. The WAL is disabled by default. To enable, set +# WalPath to where you want the WAL to be written (e.g. +# "data/mempool.wal"). +wal_dir = "" + +# Maximum number of transactions in the mempool +size = 5000 + +# Limit the total size of all txs in the mempool. +# This only accounts for raw transactions (e.g. given 1MB transactions and +# max_txs_bytes=5MB, mempool will only accept 5 transactions). +max_txs_bytes = 1073741824 + +# Size of the cache (used to filter transactions we saw earlier) in transactions +cache_size = 10000 + +# Do not remove invalid transactions from the cache (default: false) +# Set to true if it's not possible for any invalid transaction to become valid +# again in the future. +keep-invalid-txs-in-cache = false + +# Maximum size of a single transaction. +# NOTE: the max size of a tx transmitted over the network is {max_tx_bytes}. +max_tx_bytes = 1048576 + +# Maximum size of a batch of transactions to send to a peer +# Including space needed by encoding (one varint per transaction). +# XXX: Unused due to https://github.com/tendermint/tendermint/issues/5796 +max_batch_bytes = 0 + +# Experimental parameters to limit gossiping txs to up to the specified number of peers. +# We use two independent upper values for persistent and non-persistent peers. +# Unconditional peers are not affected by this feature. +# If we are connected to more than the specified number of persistent peers, only send txs to +# ExperimentalMaxGossipConnectionsToPersistentPeers of them. If one of those +# persistent peers disconnects, activate another persistent peer. +# Similarly for non-persistent peers, with an upper limit of +# ExperimentalMaxGossipConnectionsToNonPersistentPeers. +# If set to 0, the feature is disabled for the corresponding group of peers, that is, the +# number of active connections to that group of peers is not bounded. +# For non-persistent peers, if enabled, a value of 10 is recommended based on experimental +# performance results using the default P2P configuration. +experimental_max_gossip_connections_to_persistent_peers = 0 +experimental_max_gossip_connections_to_non_persistent_peers = 0 + +####################################################### +### State Sync Configuration Options ### +####################################################### +[statesync] +# State sync rapidly bootstraps a new node by discovering, fetching, and restoring a state machine +# snapshot from peers instead of fetching and replaying historical blocks. Requires some peers in +# the network to take and serve state machine snapshots. State sync is not attempted if the node +# has any local state (LastBlockHeight > 0). The node will have a truncated block history, +# starting from the height of the snapshot. +enable = false + +# RPC servers (comma-separated) for light client verification of the synced state machine and +# retrieval of state data for node bootstrapping. Also needs a trusted height and corresponding +# header hash obtained from a trusted source, and a period during which validators can be trusted. +# +# For Cosmos SDK-based chains, trust_period should usually be about 2/3 of the unbonding time (~2 +# weeks) during which they can be financially punished (slashed) for misbehavior. +rpc_servers = "" +trust_height = 0 +trust_hash = "" +trust_period = "168h0m0s" + +# Time to spend discovering snapshots before initiating a restore. +discovery_time = "15s" + +# Temporary directory for state sync snapshot chunks, defaults to the OS tempdir (typically /tmp). +# Will create a new, randomly named directory within, and remove it when done. +temp_dir = "" + +# The timeout duration before re-requesting a chunk, possibly from a different +# peer (default: 1 minute). +chunk_request_timeout = "10s" + +# The number of concurrent chunk fetchers to run (default: 1). +chunk_fetchers = "4" + +####################################################### +### Block Sync Configuration Options ### +####################################################### +[blocksync] + +# Block Sync version to use: +# +# In v0.37, v1 and v2 of the block sync protocols were deprecated. +# Please use v0 instead. +# +# 1) "v0" - the default block sync implementation +version = "v0" + +####################################################### +### Consensus Configuration Options ### +####################################################### +[consensus] + +wal_file = "data/cs.wal/wal" + +# How long we wait for a proposal block before prevoting nil +timeout_propose = "3s" +# How much timeout_propose increases with each round +timeout_propose_delta = "500ms" +# How long we wait after receiving +2/3 prevotes for “anything” (ie. not a single block or nil) +timeout_prevote = "1s" +# How much the timeout_prevote increases with each round +timeout_prevote_delta = "500ms" +# How long we wait after receiving +2/3 precommits for “anything” (ie. not a single block or nil) +timeout_precommit = "1s" +# How much the timeout_precommit increases with each round +timeout_precommit_delta = "500ms" +# How long we wait after committing a block, before starting on the new +# height (this gives us a chance to receive some more precommits, even +# though we already have +2/3). +timeout_commit = "1s" + +# How many blocks to look back to check existence of the node's consensus votes before joining consensus +# When non-zero, the node will panic upon restart +# if the same consensus key was used to sign {double_sign_check_height} last blocks. +# So, validators should stop the state machine, wait for some blocks, and then restart the state machine to avoid panic. +double_sign_check_height = 0 + +# Make progress as soon as we have all the precommits (as if TimeoutCommit = 0) +skip_timeout_commit = false + +# EmptyBlocks mode and possible interval between empty blocks +create_empty_blocks = true +create_empty_blocks_interval = "0s" + +# Reactor sleep duration parameters +peer_gossip_sleep_duration = "100ms" +peer_query_maj23_sleep_duration = "2s" + +####################################################### +### Storage Configuration Options ### +####################################################### +[storage] + +# Set to true to discard ABCI responses from the state store, which can save a +# considerable amount of disk space. Set to false to ensure ABCI responses are +# persisted. ABCI responses are required for /block_results RPC queries, and to +# reindex events in the command-line tool. +discard_abci_responses = false + +####################################################### +### Transaction Indexer Configuration Options ### +####################################################### +[tx_index] + +# What indexer to use for transactions +# +# The application will set which txs to index. In some cases a node operator will be able +# to decide which txs to index based on configuration set in the application. +# +# Options: +# 1) "null" +# 2) "kv" (default) - the simplest possible indexer, backed by key-value storage (defaults to levelDB; see DBBackend). +# - When "kv" is chosen "tx.height" and "tx.hash" will always be indexed. +# 3) "psql" - the indexer services backed by PostgreSQL. +# When "kv" or "psql" is chosen "tx.height" and "tx.hash" will always be indexed. +indexer = "kv" + +# The PostgreSQL connection configuration, the connection format: +# postgresql://:@:/? +psql-conn = "" + +####################################################### +### Instrumentation Configuration Options ### +####################################################### +[instrumentation] + +# When true, Prometheus metrics are served under /metrics on +# PrometheusListenAddr. +# Check out the documentation for the list of available metrics. +prometheus = false + +# Address to listen for Prometheus collector(s) connections +prometheus_listen_addr = ":26660" + +# Maximum number of simultaneous connections. +# If you want to accept a larger number than the default, make sure +# you increase your OS limits. +# 0 - unlimited. +max_open_connections = 3 + +# Instrumentation namespace +namespace = "cometbft" diff --git a/server/v2/types.go b/server/v2/types.go new file mode 100644 index 000000000000..034f333db76a --- /dev/null +++ b/server/v2/types.go @@ -0,0 +1,19 @@ +package serverv2 + +import ( + "github.com/spf13/viper" + + coreapp "cosmossdk.io/core/app" + "cosmossdk.io/core/transaction" + "cosmossdk.io/log" + "cosmossdk.io/server/v2/appmanager" +) + +type AppCreator[T transaction.Tx] func(log.Logger, *viper.Viper) AppI[T] + +type AppI[T transaction.Tx] interface { + GetAppManager() *appmanager.AppManager[T] + GetConsensusAuthority() string + InterfaceRegistry() coreapp.InterfaceRegistry + GetStore() any +} diff --git a/server/v2/util.go b/server/v2/util.go new file mode 100644 index 000000000000..b2a1f7f7dfa9 --- /dev/null +++ b/server/v2/util.go @@ -0,0 +1,46 @@ +package serverv2 + +import ( + "context" + "os" + + "github.com/spf13/cobra" + "github.com/spf13/viper" + + corectx "cosmossdk.io/core/context" + "cosmossdk.io/log" +) + +// SetCmdServerContext sets a command's Context value to the provided argument. +// If the context has not been set, set the given context as the default. +func SetCmdServerContext(cmd *cobra.Command, viper *viper.Viper, logger log.Logger) error { + var cmdCtx context.Context + if cmd.Context() == nil { + cmdCtx = context.Background() + } else { + cmdCtx = cmd.Context() + } + + cmd.SetContext(context.WithValue(cmdCtx, corectx.LoggerContextKey{}, logger)) + cmd.SetContext(context.WithValue(cmdCtx, corectx.ViperContextKey{}, viper)) + + return nil +} + +func GetViperFromCmd(cmd *cobra.Command) *viper.Viper { + value := cmd.Context().Value(corectx.ViperContextKey{}) + v, ok := value.(*viper.Viper) + if !ok { + return viper.New() + } + return v +} + +func GetLoggerFromCmd(cmd *cobra.Command) log.Logger { + v := cmd.Context().Value(corectx.LoggerContextKey{}) + logger, ok := v.(log.Logger) + if !ok { + return log.NewLogger(os.Stdout) + } + return logger +} diff --git a/simapp/v2/app_di.go b/simapp/v2/app_di.go index 343d71c09f4d..a91de00b55f9 100644 --- a/simapp/v2/app_di.go +++ b/simapp/v2/app_di.go @@ -5,6 +5,9 @@ import ( "os" "path/filepath" + "github.com/spf13/viper" + + coreapp "cosmossdk.io/core/app" "cosmossdk.io/core/legacy" "cosmossdk.io/core/log" "cosmossdk.io/depinject" @@ -33,6 +36,7 @@ import ( upgradekeeper "cosmossdk.io/x/upgrade/keeper" "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" servertypes "github.com/cosmos/cosmos-sdk/server/types" @@ -91,9 +95,9 @@ func AppConfig() depinject.Config { // NewSimApp returns a reference to an initialized SimApp. func NewSimApp( logger log.Logger, - appOpts servertypes.AppOptions, + viper *viper.Viper, ) *SimApp { - homeDir := appOpts.Get("home").(string) // TODO + homeDir := viper.Get(flags.FlagHome).(string) // TODO scRawDb, err := db.NewGoLevelDB("application", filepath.Join(homeDir, "data"), nil) if err != nil { panic(err) @@ -122,7 +126,7 @@ func NewSimApp( }, SCRawDB: scRawDb, }, - // appOpts, + servertypes.AppOptions(viper), // ADVANCED CONFIGURATION @@ -234,15 +238,21 @@ func (app *SimApp) AppCodec() codec.Codec { } // InterfaceRegistry returns SimApp's InterfaceRegistry. -func (app *SimApp) InterfaceRegistry() codectypes.InterfaceRegistry { +func (app *SimApp) InterfaceRegistry() coreapp.InterfaceRegistry { return app.interfaceRegistry } -// TxConfig returns SimApp's TxConfig +// TxConfig returns SimApp's TxConfig. func (app *SimApp) TxConfig() client.TxConfig { return app.txConfig } +// GetConsensusAuthority gets the consensus authority. func (app *SimApp) GetConsensusAuthority() string { return app.ConsensusParamsKeeper.GetAuthority() } + +// GetStore gets the app store. +func (app *SimApp) GetStore() any { + return app.App.GetStore() +} diff --git a/simapp/v2/go.mod b/simapp/v2/go.mod index 3ce9886492de..c6aaf71c5702 100644 --- a/simapp/v2/go.mod +++ b/simapp/v2/go.mod @@ -4,17 +4,15 @@ go 1.22.2 require ( cosmossdk.io/api v0.7.5 - cosmossdk.io/client/v2 v2.0.0-20230630094428-02b760776860 cosmossdk.io/collections v0.4.0 // indirect cosmossdk.io/core v0.12.1-0.20231114100755-569e3ff6a0d7 cosmossdk.io/depinject v1.0.0-alpha.4 cosmossdk.io/log v1.3.1 cosmossdk.io/math v1.3.0 // indirect cosmossdk.io/runtime/v2 v2.0.0-00010101000000-000000000000 - cosmossdk.io/server/v2 v2.0.0-00010101000000-000000000000 // indirect + cosmossdk.io/server/v2 v2.0.0-00010101000000-000000000000 cosmossdk.io/server/v2/cometbft v0.0.0-00010101000000-000000000000 cosmossdk.io/store/v2 v2.0.0 - cosmossdk.io/tools/confix v0.0.0-20230613133644-0a778132a60f cosmossdk.io/x/accounts v0.0.0-20240226161501-23359a0b6d91 cosmossdk.io/x/auth v0.0.0-00010101000000-000000000000 cosmossdk.io/x/authz v0.0.0-00010101000000-000000000000 @@ -33,7 +31,7 @@ require ( cosmossdk.io/x/staking v0.0.0-00010101000000-000000000000 cosmossdk.io/x/tx v0.13.3 // indirect cosmossdk.io/x/upgrade v0.0.0-20230613133644-0a778132a60f - github.com/cometbft/cometbft v1.0.0-alpha.2.0.20240530055211-ae27f7eb3c08 + github.com/cometbft/cometbft v1.0.0-alpha.2.0.20240530055211-ae27f7eb3c08 // indirect github.com/cosmos/cosmos-db v1.0.2 // this version is not used as it is always replaced by the latest Cosmos SDK version github.com/cosmos/cosmos-sdk v0.51.0 @@ -44,10 +42,15 @@ require ( github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/viper v1.19.0 github.com/stretchr/testify v1.9.0 - golang.org/x/sync v0.7.0 + golang.org/x/sync v0.7.0 // indirect google.golang.org/protobuf v1.34.2 ) +require ( + cosmossdk.io/client/v2 v2.0.0-00010101000000-000000000000 + cosmossdk.io/tools/confix v0.0.0-00010101000000-000000000000 +) + require ( buf.build/gen/go/cometbft/cometbft/protocolbuffers/go v1.34.1-20240312114316-c0d3497e35d6.1 // indirect buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.34.1-20240130113600-88ef6483f90f.1 // indirect diff --git a/simapp/v2/simdv2/cmd/commands.go b/simapp/v2/simdv2/cmd/commands.go index 699c76ca591d..d4a18c0909d8 100644 --- a/simapp/v2/simdv2/cmd/commands.go +++ b/simapp/v2/simdv2/cmd/commands.go @@ -1,23 +1,20 @@ package cmd import ( - "context" "errors" "fmt" "io" - "os" - "os/signal" - "syscall" dbm "github.com/cosmos/cosmos-db" "github.com/spf13/cobra" "github.com/spf13/viper" - "golang.org/x/sync/errgroup" "cosmossdk.io/client/v2/offchain" "cosmossdk.io/core/transaction" "cosmossdk.io/log" runtimev2 "cosmossdk.io/runtime/v2" + serverv2 "cosmossdk.io/server/v2" + "cosmossdk.io/server/v2/api/grpc" "cosmossdk.io/server/v2/cometbft" "cosmossdk.io/simapp/v2" confixcmd "cosmossdk.io/tools/confix/cmd" @@ -28,9 +25,6 @@ import ( "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/client/keys" "github.com/cosmos/cosmos-sdk/client/rpc" - "github.com/cosmos/cosmos-sdk/codec" - codectypes "github.com/cosmos/cosmos-sdk/codec/types" - // TODO migrate all server dependencies to server/v2 "github.com/cosmos/cosmos-sdk/server" servertypes "github.com/cosmos/cosmos-sdk/server/types" sdk "github.com/cosmos/cosmos-sdk/types" @@ -54,11 +48,17 @@ func (t *temporaryTxDecoder) DecodeJSON(bz []byte) (transaction.Tx, error) { return t.txConfig.TxJSONDecoder()(bz) } +func newApp( + logger log.Logger, + viper *viper.Viper, +) serverv2.AppI[transaction.Tx] { + sa := simapp.NewSimApp(logger, viper) + return sa +} + func initRootCmd( rootCmd *cobra.Command, txConfig client.TxConfig, - _ codectypes.InterfaceRegistry, - _ codec.Codec, moduleManager *runtimev2.MM, v1moduleManager *module.Manager, ) { @@ -69,11 +69,26 @@ func initRootCmd( genutilcli.InitCmd(moduleManager), debug.Cmd(), confixcmd.ConfigCommand(), - startCommand(&temporaryTxDecoder{txConfig}), - // TODO pruning.Cmd(newApp), - // TODO snapshot.Cmd(newApp), + // pruning.Cmd(newApp), // TODO add to comet server + // snapshot.Cmd(newApp), // TODO add to comet server ) + logger, err := serverv2.NewLogger(viper.New(), rootCmd.OutOrStdout()) + if err != nil { + panic(fmt.Sprintf("failed to create logger: %v", err)) + } + + // Add empty server struct here for writing default config + if err = serverv2.AddCommands( + rootCmd, + newApp, + logger, + cometbft.New(&temporaryTxDecoder{txConfig}), + grpc.New(), + ); err != nil { + panic(err) + } + // add keybase, auxiliary RPC, query, genesis, and tx child commands rootCmd.AddCommand( server.StatusCommand(), @@ -85,57 +100,29 @@ func initRootCmd( ) } -func startCommand(txCodec transaction.Codec[transaction.Tx]) *cobra.Command { - cmd := &cobra.Command{ - Use: "start", - Short: "Start the application", - RunE: func(cmd *cobra.Command, args []string) error { - serverCtx := server.GetServerContextFromCmd(cmd) - sa := simapp.NewSimApp(serverCtx.Logger, serverCtx.Viper) - am := sa.App.AppManager - serverCfg := cometbft.Config{CmtConfig: serverCtx.Config, ConsensusAuthority: sa.GetConsensusAuthority()} - - cometServer := cometbft.NewCometBFTServer[transaction.Tx]( - am, - sa.GetStore(), - sa.GetLogger(), - serverCfg, - txCodec, - ) - ctx := cmd.Context() - ctx, cancelFn := context.WithCancel(ctx) - g, _ := errgroup.WithContext(ctx) - g.Go(func() error { - sigCh := make(chan os.Signal, 1) - signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM) - sig := <-sigCh - cancelFn() - cmd.Printf("caught %s signal\n", sig.String()) - - if err := cometServer.Stop(ctx); err != nil { - cmd.PrintErrln("failed to stop servers:", err) - } - return nil - }) - - if err := cometServer.Start(ctx); err != nil { - return fmt.Errorf("failed to start servers: %w", err) - } - return g.Wait() - }, - } - return cmd -} - // genesisCommand builds genesis-related `simd genesis` command. Users may provide application specific commands as a parameter func genesisCommand( txConfig client.TxConfig, moduleManager *module.Manager, - appExport servertypes.AppExporter, + appExport func(logger log.Logger, + height int64, + forZeroHeight bool, + jailAllowedAddrs []string, + viper *viper.Viper, + modulesToExport []string, + ) (servertypes.ExportedApp, error), cmds ...*cobra.Command, ) *cobra.Command { - cmd := genutilcli.Commands(txConfig, moduleManager, appExport) + compatAppExporter := func(logger log.Logger, db dbm.DB, traceWriter io.Writer, height int64, forZeroHeight bool, jailAllowedAddrs []string, appOpts servertypes.AppOptions, modulesToExport []string) (servertypes.ExportedApp, error) { + viperAppOpts, ok := appOpts.(*viper.Viper) + if !ok { + return servertypes.ExportedApp{}, errors.New("appOpts is not viper.Viper") + } + + return appExport(logger, height, forZeroHeight, jailAllowedAddrs, viperAppOpts, modulesToExport) + } + cmd := genutilcli.Commands(txConfig, moduleManager, compatAppExporter) for _, subCmd := range cmds { cmd.AddCommand(subCmd) } @@ -191,50 +178,31 @@ func txCommand() *cobra.Command { // appExport creates a new simapp (optionally at a given height) and exports state. func appExport( logger log.Logger, - _ dbm.DB, - _ io.Writer, height int64, forZeroHeight bool, jailAllowedAddrs []string, - appOpts servertypes.AppOptions, + viper *viper.Viper, modulesToExport []string, ) (servertypes.ExportedApp, error) { // this check is necessary as we use the flag in x/upgrade. // we can exit more gracefully by checking the flag here. - homePath, ok := appOpts.Get(flags.FlagHome).(string) + homePath, ok := viper.Get(flags.FlagHome).(string) if !ok || homePath == "" { return servertypes.ExportedApp{}, errors.New("application home not set") } - - viperAppOpts, ok := appOpts.(*viper.Viper) - if !ok { - return servertypes.ExportedApp{}, errors.New("appOpts is not viper.Viper") - } - // overwrite the FlagInvCheckPeriod - viperAppOpts.Set(server.FlagInvCheckPeriod, 1) - appOpts = viperAppOpts + viper.Set(server.FlagInvCheckPeriod, 1) var simApp *simapp.SimApp if height != -1 { - simApp = simapp.NewSimApp(logger, appOpts) + simApp = simapp.NewSimApp(logger, viper) if err := simApp.LoadHeight(uint64(height)); err != nil { return servertypes.ExportedApp{}, err } } else { - simApp = simapp.NewSimApp(logger, appOpts) + simApp = simapp.NewSimApp(logger, viper) } return simApp.ExportAppStateAndValidators(forZeroHeight, jailAllowedAddrs, modulesToExport) } - -var tempDir = func() string { - dir, err := os.MkdirTemp("", "simapp") - if err != nil { - dir = simapp.DefaultNodeHome - } - defer os.RemoveAll(dir) - - return dir -} diff --git a/simapp/v2/simdv2/cmd/config.go b/simapp/v2/simdv2/cmd/config.go index cda73a922dd1..0794a8a42a79 100644 --- a/simapp/v2/simdv2/cmd/config.go +++ b/simapp/v2/simdv2/cmd/config.go @@ -3,28 +3,10 @@ package cmd import ( "strings" - cmtcfg "github.com/cometbft/cometbft/config" - clientconfig "github.com/cosmos/cosmos-sdk/client/config" "github.com/cosmos/cosmos-sdk/crypto/keyring" - serverconfig "github.com/cosmos/cosmos-sdk/server/config" ) -// initCometBFTConfig helps to override default CometBFT Config values. -// return cmtcfg.DefaultConfig if no custom configuration is required for the application. -func initCometBFTConfig() *cmtcfg.Config { - cfg := cmtcfg.DefaultConfig() - - // only display only error logs by default except for p2p and state - cfg.LogLevel = "*:error,p2p:info,state:info" - - // these values put a higher strain on node memory - // cfg.P2P.MaxNumInboundPeers = 100 - // cfg.P2P.MaxNumOutboundPeers = 40 - - return cfg -} - // initAppConfig helps to override default client config template and configs. // return "", nil if no custom configuration is required for the application. func initClientConfig() (string, interface{}) { @@ -67,60 +49,3 @@ gas-adjustment = {{ .GasConfig.GasAdjustment }} return customClientConfigTemplate, customClientConfig } - -// initAppConfig helps to override default appConfig template and configs. -// return "", nil if no custom configuration is required for the application. -func initAppConfig() (string, interface{}) { - // The following code snippet is just for reference. - - // CustomConfig defines an arbitrary custom config to extend app.toml. - // If you don't need it, you can remove it. - // If you wish to add fields that correspond to flags that aren't in the SDK server config, - // this custom config can as well help. - type CustomConfig struct { - CustomField string `mapstructure:"custom-field"` - } - - type CustomAppConfig struct { - serverconfig.Config `mapstructure:",squash"` - - Custom CustomConfig `mapstructure:"custom"` - } - - // Optionally allow the chain developer to overwrite the SDK's default - // server config. - srvCfg := serverconfig.DefaultConfig() - // The SDK's default minimum gas price is set to "" (empty value) inside - // app.toml. If left empty by validators, the node will halt on startup. - // However, the chain developer can set a default app.toml value for their - // validators here. - // - // In summary: - // - if you leave srvCfg.MinGasPrices = "", all validators MUST tweak their - // own app.toml config, - // - if you set srvCfg.MinGasPrices non-empty, validators CAN tweak their - // own app.toml to override, or use this default value. - // - // In simapp, we set the min gas prices to 0. - srvCfg.MinGasPrices = "0stake" - // srvCfg.BaseConfig.IAVLDisableFastNode = true // disable fastnode by default - - // Now we set the custom config default values. - customAppConfig := CustomAppConfig{ - Config: *srvCfg, - Custom: CustomConfig{ - CustomField: "anything", - }, - } - - // The default SDK app template is defined in serverconfig.DefaultConfigTemplate. - // We append the custom config template to the default one. - // And we set the default config to the custom app template. - customAppTemplate := serverconfig.DefaultConfigTemplate + ` -[custom] -# That field will be parsed by server.InterceptConfigsPreRunHandler and held by viper. -# Do not forget to add quotes around the value if it is a string. -custom-field = "{{ .Custom.CustomField }}"` - - return customAppTemplate, customAppConfig -} diff --git a/simapp/v2/simdv2/cmd/root_di.go b/simapp/v2/simdv2/cmd/root_di.go index e78f3a68f1fd..029383696047 100644 --- a/simapp/v2/simdv2/cmd/root_di.go +++ b/simapp/v2/simdv2/cmd/root_di.go @@ -6,7 +6,6 @@ import ( "github.com/spf13/cobra" "cosmossdk.io/client/v2/autocli" - clientv2keyring "cosmossdk.io/client/v2/autocli/keyring" "cosmossdk.io/core/address" "cosmossdk.io/core/appmodule/v2" "cosmossdk.io/core/legacy" @@ -22,10 +21,7 @@ import ( "github.com/cosmos/cosmos-sdk/client/config" "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" - "github.com/cosmos/cosmos-sdk/crypto/keyring" - "github.com/cosmos/cosmos-sdk/server" // TODO: remove me: https://github.com/cosmos/cosmos-sdk/pull/20412/files#r1622878528 "github.com/cosmos/cosmos-sdk/std" - simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" "github.com/cosmos/cosmos-sdk/types/module" ) @@ -41,17 +37,13 @@ func NewRootCmd() *cobra.Command { if err := depinject.Inject( depinject.Configs( simapp.AppConfig(), - depinject.Supply( - log.NewNopLogger(), - simtestutil.NewAppOptionsWithFlagHome(tempDir()), - ), + depinject.Supply(log.NewNopLogger()), depinject.Provide( codec.ProvideInterfaceRegistry, codec.ProvideAddressCodec, codec.ProvideProtoCodec, codec.ProvideLegacyAmino, ProvideClientContext, - ProvideKeyring, ProvideV1ModuleManager, ), depinject.Invoke( @@ -92,21 +84,16 @@ func NewRootCmd() *cobra.Command { return err } - customAppTemplate, customAppConfig := initAppConfig() - customCMTConfig := initCometBFTConfig() - - return server.InterceptConfigsPreRunHandler(cmd, customAppTemplate, customAppConfig, customCMTConfig) + return nil }, } initRootCmd( rootCmd, clientCtx.TxConfig, - clientCtx.InterfaceRegistry, - clientCtx.Codec, moduleManager, - v1ModuleManager) - + v1ModuleManager, + ) if err := autoCliOpts.EnhanceRootCommand(rootCmd); err != nil { panic(err) } @@ -160,15 +147,6 @@ func ProvideClientContext( return clientCtx } -func ProvideKeyring(clientCtx client.Context, addressCodec address.Codec) (clientv2keyring.Keyring, error) { - kb, err := client.NewKeyringFromBackend(clientCtx, clientCtx.Keyring.Backend()) - if err != nil { - return nil, err - } - - return keyring.NewAutoCLIKeyring(kb) -} - func ProvideV1ModuleManager(modules map[string]appmodule.AppModule) *module.Manager { return module.NewManagerFromMap(modules) } diff --git a/simapp/v2/simdv2/main.go b/simapp/v2/simdv2/main.go index fc356185c4e6..c327f3e84ed7 100644 --- a/simapp/v2/simdv2/main.go +++ b/simapp/v2/simdv2/main.go @@ -7,7 +7,7 @@ import ( "cosmossdk.io/simapp/v2" "cosmossdk.io/simapp/v2/simdv2/cmd" - svrcmd "github.com/cosmos/cosmos-sdk/server/cmd" + svrcmd "github.com/cosmos/cosmos-sdk/server/cmd" // TODO(@julienrbrt), no need to abstract this. ) func main() { diff --git a/x/genutil/client/cli/commands.go b/x/genutil/client/cli/commands.go index 5e1f671b77fc..289daf2b2789 100644 --- a/x/genutil/client/cli/commands.go +++ b/x/genutil/client/cli/commands.go @@ -12,6 +12,9 @@ import ( genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types" ) +// TODO(serverv2): DO NOT DEPEND ON v1 module manager +// TODO(serverv2): remove app exporter notion that is server v1 specific + // Commands adds core sdk's sub-commands into genesis command. func Commands(txConfig client.TxConfig, mm *module.Manager, appExport servertypes.AppExporter) *cobra.Command { return CommandsWithCustomMigrationMap(txConfig, mm, appExport, MigrationMap)