Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(server/v2): kill viper from server components #21663

Merged
merged 3 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions server/v2/api/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/cosmos/gogoproto/proto"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
Expand Down Expand Up @@ -47,19 +46,19 @@ func New[T transaction.Tx](cfgOptions ...CfgOption) *Server[T] {

// Init returns a correctly configured and initialized gRPC server.
// Note, the caller is responsible for starting the server.
func (s *Server[T]) Init(appI serverv2.AppI[T], v *viper.Viper, logger log.Logger) error {
cfg := s.Config().(*Config)
if v != nil {
if err := serverv2.UnmarshalSubConfig(v, s.Name(), &cfg); err != nil {
func (s *Server[T]) Init(appI serverv2.AppI[T], cfg map[string]any, logger log.Logger) error {
serverCfg := s.Config().(*Config)
if len(cfg) > 0 {
if err := serverv2.UnmarshalSubConfig(cfg, s.Name(), &serverCfg); err != nil {
return fmt.Errorf("failed to unmarshal config: %w", err)
}
}
methodsMap := appI.GetGPRCMethodsToMessageMap()

grpcSrv := grpc.NewServer(
grpc.ForceServerCodec(newProtoCodec(appI.InterfaceRegistry()).GRPCCodec()),
grpc.MaxSendMsgSize(cfg.MaxSendMsgSize),
grpc.MaxRecvMsgSize(cfg.MaxRecvMsgSize),
grpc.MaxSendMsgSize(serverCfg.MaxSendMsgSize),
grpc.MaxRecvMsgSize(serverCfg.MaxRecvMsgSize),
grpc.UnknownServiceHandler(
makeUnknownServiceHandler(methodsMap, appI.GetAppManager()),
),
Expand All @@ -69,7 +68,7 @@ func (s *Server[T]) Init(appI serverv2.AppI[T], v *viper.Viper, logger log.Logge
gogoreflection.Register(grpcSrv, slices.Collect(maps.Keys(methodsMap)), logger.With("sub-module", "grpc-reflection"))

s.grpcSrv = grpcSrv
s.config = cfg
s.config = serverCfg
s.logger = logger.With(log.ModuleKey, s.Name())

return nil
Expand Down
11 changes: 5 additions & 6 deletions server/v2/api/grpcgateway/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/cosmos/gogoproto/jsonpb"
"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/viper"
"google.golang.org/grpc"

"cosmossdk.io/core/transaction"
Expand Down Expand Up @@ -83,10 +82,10 @@ func (s *GRPCGatewayServer[T]) Config() any {
return s.config
}

func (s *GRPCGatewayServer[T]) Init(appI serverv2.AppI[transaction.Tx], v *viper.Viper, logger log.Logger) error {
cfg := s.Config().(*Config)
if v != nil {
if err := serverv2.UnmarshalSubConfig(v, s.Name(), &cfg); err != nil {
func (s *GRPCGatewayServer[T]) Init(appI serverv2.AppI[transaction.Tx], cfg map[string]any, logger log.Logger) error {
serverCfg := s.Config().(*Config)
if len(cfg) > 0 {
if err := serverv2.UnmarshalSubConfig(cfg, s.Name(), &serverCfg); err != nil {
return fmt.Errorf("failed to unmarshal config: %w", err)
}
}
Expand All @@ -95,7 +94,7 @@ func (s *GRPCGatewayServer[T]) Init(appI serverv2.AppI[transaction.Tx], v *viper
// appI.RegisterGRPCGatewayRoutes(s.GRPCGatewayRouter, s.GRPCSrv)

s.logger = logger
s.config = cfg
s.config = serverCfg

return nil
}
Expand Down
14 changes: 0 additions & 14 deletions server/v2/cometbft/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package cometbft

import (
cmtcfg "github.com/cometbft/cometbft/config"
"github.com/spf13/viper"

serverv2 "cosmossdk.io/server/v2"
)

// Config is the configuration for the CometBFT application
Expand Down Expand Up @@ -53,14 +50,3 @@ func OverwriteDefaultAppTomlConfig(newCfg *AppTomlConfig) CfgOption {
cfg.AppTomlConfig = newCfg
}
}

func getConfigTomlFromViper(v *viper.Viper) *cmtcfg.Config {
rootDir := v.GetString(serverv2.FlagHome)

conf := cmtcfg.DefaultConfig()
if err := v.Unmarshal(conf); err != nil {
return cmtcfg.DefaultConfig().SetRoot(rootDir)
}

return conf.SetRoot(rootDir)
}
2 changes: 1 addition & 1 deletion server/v2/cometbft/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ 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
github.com/stretchr/testify v1.9.0
google.golang.org/genproto/googleapis/api v0.0.0-20240604185151-ef581f913117
google.golang.org/grpc v1.66.1
Expand Down Expand Up @@ -150,6 +149,7 @@ require (
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.7.0 // indirect
github.com/spf13/viper v1.19.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/supranational/blst v0.3.13 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect
Expand Down
21 changes: 14 additions & 7 deletions server/v2/cometbft/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/cometbft/cometbft/proxy"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"

"cosmossdk.io/core/transaction"
"cosmossdk.io/log"
Expand Down Expand Up @@ -58,23 +57,31 @@ func New[T transaction.Tx](txCodec transaction.Codec[T], serverOptions ServerOpt
}
}

func (s *CometBFTServer[T]) Init(appI serverv2.AppI[T], v *viper.Viper, logger log.Logger) error {
func (s *CometBFTServer[T]) Init(appI serverv2.AppI[T], cfg map[string]any, logger log.Logger) error {
home, _ := cfg[serverv2.FlagHome].(string)

// get configs (app.toml + config.toml) from viper
appTomlConfig := s.Config().(*AppTomlConfig)
if v != nil {
if err := serverv2.UnmarshalSubConfig(v, s.Name(), &appTomlConfig); err != nil {
configTomlConfig := cmtcfg.DefaultConfig().SetRoot(home)
if len(cfg) > 0 {
if err := serverv2.UnmarshalSubConfig(cfg, s.Name(), &appTomlConfig); err != nil {
return fmt.Errorf("failed to unmarshal config: %w", err)
}

if err := serverv2.UnmarshalSubConfig(cfg, "", &configTomlConfig); err != nil {
return fmt.Errorf("failed to unmarshal config: %w", err)
}
}

s.config = Config{
ConfigTomlConfig: getConfigTomlFromViper(v),
ConfigTomlConfig: configTomlConfig,
AppTomlConfig: appTomlConfig,
}

chainID := v.GetString(FlagChainID)
chainID, _ := cfg[FlagChainID].(string)
if chainID == "" {
// fallback to genesis chain-id
reader, err := os.Open(filepath.Join(v.GetString(serverv2.FlagHome), "config", "genesis.json"))
reader, err := os.Open(filepath.Join(home, "config", "genesis.json"))
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion server/v2/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func createStartCommand[T transaction.Tx](
return err
}

if err := server.Init(newApp(l, v), v, l); err != nil {
if err := server.Init(newApp(l, v), v.AllSettings(), l); err != nil {
return err
}

Expand Down
19 changes: 11 additions & 8 deletions server/v2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,19 @@ func ReadConfig(configPath string) (*viper.Viper, error) {
return v, nil
}

// UnmarshalSubConfig unmarshals the given subconfig from the viper instance.
// It unmarshals the config, env, flags into the target struct.
// Use this instead of viper.Sub because viper does not unmarshal flags.
func UnmarshalSubConfig(v *viper.Viper, subName string, target any) error {
// UnmarshalSubConfig unmarshals the given (sub) config from the main config (given as a map) into the target.
// If subName is empty, the main config is unmarshaled into the target.
func UnmarshalSubConfig(cfg map[string]any, subName string, target any) error {
var sub any
for k, val := range v.AllSettings() {
if k == subName {
sub = val
break
if subName != "" {
for k, val := range cfg {
if k == subName {
sub = val
break
}
}
} else {
sub = cfg
}

// Create a new decoder with custom decoding options
Expand Down
5 changes: 3 additions & 2 deletions server/v2/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,17 @@ func TestUnmarshalSubConfig(t *testing.T) {

v, err := serverv2.ReadConfig(configPath)
require.NoError(t, err)
cfg := v.AllSettings()

grpcConfig := grpc.DefaultConfig()
err = serverv2.UnmarshalSubConfig(v, "grpc", &grpcConfig)
err = serverv2.UnmarshalSubConfig(cfg, "grpc", &grpcConfig)
require.NoError(t, err)

require.True(t, grpc.DefaultConfig().Enable)
require.False(t, grpcConfig.Enable)

storeConfig := store.Config{}
err = serverv2.UnmarshalSubConfig(v, "store", &storeConfig)
err = serverv2.UnmarshalSubConfig(cfg, "store", &storeConfig)
require.NoError(t, err)
require.Equal(t, *store.DefaultConfig(), storeConfig)
}
15 changes: 7 additions & 8 deletions server/v2/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/pelletier/go-toml/v2"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"golang.org/x/sync/errgroup"

"cosmossdk.io/core/transaction"
Expand All @@ -23,7 +22,7 @@ type ServerComponent[T transaction.Tx] interface {

Start(context.Context) error
Stop(context.Context) error
Init(AppI[T], *viper.Viper, log.Logger) error
Init(AppI[T], map[string]any, log.Logger) error
}

// HasStartFlags is a server module that has start flags.
Expand Down Expand Up @@ -189,25 +188,25 @@ func (s *Server[T]) StartCmdFlags() *pflag.FlagSet {

// 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 {
func (s *Server[T]) Init(appI AppI[T], cfg map[string]any, logger log.Logger) error {
serverCfg := s.config
if len(cfg) > 0 {
if err := UnmarshalSubConfig(cfg, s.Name(), &serverCfg); err != nil {
return fmt.Errorf("failed to unmarshal config: %w", err)
}
}

var components []ServerComponent[T]
for _, mod := range s.components {
mod := mod
if err := mod.Init(appI, v, logger); err != nil {
if err := mod.Init(appI, cfg, logger); err != nil {
return err
}

components = append(components, mod)
}

s.config = cfg
s.config = serverCfg
s.components = components
return nil
}
Expand Down
4 changes: 1 addition & 3 deletions server/v2/server_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"fmt"
"math/rand"

"github.com/spf13/viper"

"cosmossdk.io/core/transaction"
"cosmossdk.io/log"
serverv2 "cosmossdk.io/server/v2"
Expand All @@ -33,7 +31,7 @@ func (s *mockServer) Name() string {
return s.name
}

func (s *mockServer) Init(appI serverv2.AppI[transaction.Tx], v *viper.Viper, logger log.Logger) error {
func (s *mockServer) Init(appI serverv2.AppI[transaction.Tx], cfg map[string]any, logger log.Logger) error {
return nil
}

Expand Down
5 changes: 3 additions & 2 deletions server/v2/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,15 @@ func TestServer(t *testing.T) {
if err != nil {
v = viper.New()
}
cfg := v.AllSettings()

logger := log.NewLogger(os.Stdout)
grpcServer := grpc.New[transaction.Tx]()
err = grpcServer.Init(&mockApp[transaction.Tx]{}, v, logger)
err = grpcServer.Init(&mockApp[transaction.Tx]{}, cfg, logger)
require.NoError(t, err)

storeServer := store.New[transaction.Tx](nil /* nil appCreator as not using CLI commands */)
err = storeServer.Init(&mockApp[transaction.Tx]{}, v, logger)
err = storeServer.Init(&mockApp[transaction.Tx]{}, cfg, logger)
require.NoError(t, err)

mockServer := &mockServer{name: "mock-server-1", ch: make(chan string, 100)}
Expand Down
11 changes: 5 additions & 6 deletions server/v2/store/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"

"github.com/spf13/cobra"
"github.com/spf13/viper"

"cosmossdk.io/core/transaction"
"cosmossdk.io/log"
Expand All @@ -24,14 +23,14 @@ func New[T transaction.Tx](appCreator serverv2.AppCreator[T]) *StoreComponent[T]
return &StoreComponent[T]{appCreator: appCreator}
}

func (s *StoreComponent[T]) Init(appI serverv2.AppI[T], v *viper.Viper, logger log.Logger) error {
cfg := DefaultConfig()
if v != nil {
if err := serverv2.UnmarshalSubConfig(v, s.Name(), &cfg); err != nil {
func (s *StoreComponent[T]) Init(appI serverv2.AppI[T], cfg map[string]any, logger log.Logger) error {
serverCfg := DefaultConfig()
if len(cfg) > 0 {
if err := serverv2.UnmarshalSubConfig(cfg, s.Name(), &serverCfg); err != nil {
return fmt.Errorf("failed to unmarshal config: %w", err)
}
}
s.config = cfg
s.config = serverCfg
return nil
}

Expand Down
Loading