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): eager config loading #22267

Merged
merged 55 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
b2072de
begin config rationalization
kocubinski Oct 11, 2024
ecf73cf
the concept is proven
kocubinski Oct 13, 2024
8cb7307
interesting, start command works
kocubinski Oct 14, 2024
3803716
even more clean up
kocubinski Oct 15, 2024
30d1789
genesis export working
kocubinski Oct 15, 2024
23780e2
only build store in app builder
kocubinski Oct 15, 2024
da16c69
wipg
kocubinski Oct 23, 2024
ddfac2b
Merge branch 'main' of github.com:cosmos/cosmos-sdk into kocu/config-rf
kocubinski Oct 23, 2024
9c38263
big progress!
kocubinski Oct 23, 2024
cf96c1f
fix testnets command
kocubinski Oct 23, 2024
d3012f1
fix simapp/v2 tests
kocubinski Oct 23, 2024
9ca9e6b
adapt for runtime/v1
kocubinski Oct 23, 2024
43c10ad
rm dead method
kocubinski Oct 23, 2024
4c93ad7
Merge branch 'main' of github.com:cosmos/cosmos-sdk into kocu/config-rf
kocubinski Oct 23, 2024
3e0ded0
rm Init() method
kocubinski Oct 23, 2024
72b278a
clean up
kocubinski Oct 23, 2024
a3c111f
go mod tidy all
kocubinski Oct 23, 2024
b689605
lint fix
kocubinski Oct 23, 2024
10e2d4b
moar dep fix
kocubinski Oct 23, 2024
b894a32
default comet config
kocubinski Oct 23, 2024
be3a0b3
Merge branch 'main' of github.com:cosmos/cosmos-sdk into kocu/config-rf
kocubinski Oct 24, 2024
fc68979
minor clean up
kocubinski Oct 24, 2024
348028f
refactor
kocubinski Oct 24, 2024
40bfd6b
demo consensus override
kocubinski Oct 24, 2024
68f543f
changelog
kocubinski Oct 24, 2024
bb95533
fix
kocubinski Oct 24, 2024
e4047cc
Merge branch 'main' into kocu/config-rf
kocubinski Oct 25, 2024
0f79666
clean up
kocubinski Oct 25, 2024
df178ab
Merge branch 'main' of github.com:cosmos/cosmos-sdk into kocu/config-rf
kocubinski Oct 25, 2024
9055ee2
Merge branch 'kocu/config-rf' of github.com:cosmos/cosmos-sdk into ko…
kocubinski Oct 25, 2024
d39386d
Merge branch 'main' of github.com:cosmos/cosmos-sdk into kocu/config-rf
kocubinski Oct 25, 2024
dcc3ddc
rm core replaces
kocubinski Oct 25, 2024
17eadc8
Merge branch 'main' of github.com:cosmos/cosmos-sdk into kocu/config-rf
kocubinski Oct 25, 2024
ca06258
use factory pattern instead
kocubinski Oct 25, 2024
20e7ec1
fix factory pattern
kocubinski Oct 25, 2024
94d30f5
rm redundant
kocubinski Oct 25, 2024
ff0fa66
revert .vscode change
kocubinski Oct 25, 2024
9e60b5e
add call
kocubinski Oct 25, 2024
3a83516
rm comment
kocubinski Oct 25, 2024
b21b825
rm verbose
kocubinski Oct 25, 2024
ce56ae9
rm annotation, add helper command to server/v2
kocubinski Oct 28, 2024
4380b02
Merge branch 'main' of github.com:cosmos/cosmos-sdk into kocu/config-rf
kocubinski Oct 28, 2024
d3bb0c3
rf remove redudnancy
kocubinski Oct 28, 2024
c48172d
typos
kocubinski Oct 28, 2024
9addef4
minor cleanup
kocubinski Oct 28, 2024
e9c4005
export two functions
kocubinski Oct 28, 2024
6d18467
add CODEOWNER entry for server/v2
kocubinski Oct 28, 2024
d200520
add comments
kocubinski Oct 28, 2024
13d9771
mv helper fn
kocubinski Oct 28, 2024
1cadeeb
rm helper fn
kocubinski Oct 28, 2024
6472136
fix
kocubinski Oct 28, 2024
9c38df2
grpcgateway fix
kocubinski Oct 28, 2024
7014d0d
rename file
kocubinski Oct 28, 2024
6272619
typos and exports
kocubinski Oct 28, 2024
e7603cf
configurable homedir in ClientCtx, remove global var
kocubinski Oct 28, 2024
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
24 changes: 24 additions & 0 deletions client/v2/helpers/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,27 @@ func GetNodeHomeDirectory(name string) (string, error) {

return filepath.Join(userHomeDir, name), nil
}

func DefaultHomeDir(name string) (string, error) {
kocubinski marked this conversation as resolved.
Show resolved Hide resolved
// get the home directory from the environment variable
// to not clash with the $HOME system variable, when no prefix is set
// we check the NODE_HOME environment variable
envPrefix := EnvPrefix
homeDir, envHome := "", "HOME"
if len(envPrefix) > 0 {
homeDir = os.Getenv(envPrefix + "_" + envHome)
} else {
homeDir = os.Getenv("NODE_" + envHome)
}
if homeDir != "" {
return filepath.Clean(homeDir), nil
}

// get user home directory
userHomeDir, err := os.UserHomeDir()
if err != nil {
return "", err
}

return filepath.Join(userHomeDir, name), nil
}
kocubinski marked this conversation as resolved.
Show resolved Hide resolved
13 changes: 13 additions & 0 deletions core/server/config.go
kocubinski marked this conversation as resolved.
Show resolved Hide resolved
kocubinski marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,16 @@ type DynamicConfig interface {
Get(string) any
GetString(string) string
}

type ConfigMap map[string]any

func (c ConfigMap) Get(s string) any {
kocubinski marked this conversation as resolved.
Show resolved Hide resolved
return c[s]
}

type ModuleConfigMap struct {
Module string
Config ConfigMap
}

func (ModuleConfigMap) IsManyPerContainerType() {}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ require (
replace (
cosmossdk.io/api => ./api
cosmossdk.io/collections => ./collections
cosmossdk.io/core => ./core
cosmossdk.io/store => ./store
cosmossdk.io/x/bank => ./x/bank
cosmossdk.io/x/staking => ./x/staking
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.35.1-20240130113600-88e
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.35.1-20240130113600-88ef6483f90f.1/go.mod h1:zqi/LZjZhyvjCMTEVIwAf5VRlkLduuCfqmZxgoormq0=
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/core v1.0.0-alpha.4 h1:9iuroT9ejDYETCsGkzkvs/wAY/5UFl7nCIINFRxyMJY=
cosmossdk.io/core v1.0.0-alpha.4/go.mod h1:3u9cWq1FAVtiiCrDPpo4LhR+9V6k/ycSG4/Y/tREWCY=
cosmossdk.io/core/testing v0.0.0-20240923163230-04da382a9f29 h1:NxxUo0GMJUbIuVg0R70e3cbn9eFTEuMr7ev1AFvypdY=
cosmossdk.io/core/testing v0.0.0-20240923163230-04da382a9f29/go.mod h1:8s2tPeJtSiQuoyPmr2Ag7meikonISO4Fv4MoO8+ORrs=
cosmossdk.io/depinject v1.0.0 h1:dQaTu6+O6askNXO06+jyeUAnF2/ssKwrrszP9t5q050=
Expand Down
10 changes: 6 additions & 4 deletions runtime/v2/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
type AppBuilder[T transaction.Tx] struct {
app *App[T]
storeBuilder root.Builder
storeConfig *root.Config

// the following fields are used to overwrite the default
branch func(state store.ReaderMap) store.WriterMap
Expand Down Expand Up @@ -82,12 +83,13 @@ func (a *AppBuilder[T]) Build(opts ...AppBuilderOption[T]) (*App[T], error) {
}
}

a.app.db = a.storeBuilder.Get()
if a.app.db == nil {
return nil, fmt.Errorf("storeBuilder did not return a db")
var err error
a.app.db, err = a.storeBuilder.Build(a.app.logger, a.storeConfig)
if err != nil {
return nil, err
}

if err := a.app.moduleManager.RegisterServices(a.app); err != nil {
if err = a.app.moduleManager.RegisterServices(a.app); err != nil {
return nil, err
}

Expand Down
1 change: 1 addition & 0 deletions runtime/v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ replace (
cosmossdk.io/server/v2/stf => ../../server/v2/stf
cosmossdk.io/store/v2 => ../../store/v2
cosmossdk.io/x/tx => ../../x/tx
cosmossdk.io/core => ../../core
)

require (
Expand Down
4 changes: 3 additions & 1 deletion runtime/v2/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func ProvideAppBuilder[T transaction.Tx](
interfaceRegistrar registry.InterfaceRegistrar,
amino registry.AminoRegistrar,
storeBuilder root.Builder,
storeConfig *root.Config,
) (
*AppBuilder[T],
*stf.MsgRouterBuilder,
Expand All @@ -134,14 +135,15 @@ func ProvideAppBuilder[T transaction.Tx](
queryHandlers: map[string]appmodulev2.Handler{},
storeLoader: DefaultStoreLoader,
}
appBuilder := &AppBuilder[T]{app: app, storeBuilder: storeBuilder}
appBuilder := &AppBuilder[T]{app: app, storeBuilder: storeBuilder, storeConfig: storeConfig}

return appBuilder, msgRouterBuilder, appModule[T]{app}, protoFiles, protoTypes
}

type AppInputs struct {
depinject.In

StoreConfig *root.Config
Config *runtimev2.Module
AppBuilder *AppBuilder[transaction.Tx]
ModuleManager *MM[transaction.Tx]
Expand Down
50 changes: 30 additions & 20 deletions server/v2/api/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"strings"
"sync"

gogoproto "github.com/cosmos/gogoproto/proto"
"github.com/spf13/pflag"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
Expand All @@ -21,10 +20,13 @@ import (
"google.golang.org/protobuf/reflect/protoreflect"

appmodulev2 "cosmossdk.io/core/appmodule/v2"
"cosmossdk.io/core/server"
"cosmossdk.io/core/transaction"
"cosmossdk.io/log"
serverv2 "cosmossdk.io/server/v2"
"cosmossdk.io/server/v2/api/grpc/gogoreflection"

gogoproto "github.com/cosmos/gogoproto/proto"
)

const (
Expand All @@ -42,38 +44,46 @@ type Server[T transaction.Tx] struct {
}

// New creates a new grpc server.
func New[T transaction.Tx](cfgOptions ...CfgOption) *Server[T] {
return &Server[T]{
func New[T transaction.Tx](
logger log.Logger,
interfaceRegistry server.InterfaceRegistry,
queryHandlers map[string]appmodulev2.Handler,
queryable interface {
Query(ctx context.Context, version uint64, msg transaction.Msg) (transaction.Msg, error)
},
cfg server.ConfigMap,
cfgOptions ...CfgOption,
) (*Server[T], error) {
srv := &Server[T]{
cfgOptions: cfgOptions,
}
}

// 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], cfg map[string]any, logger log.Logger) error {
serverCfg := s.Config().(*Config)
serverCfg := srv.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)
if err := serverv2.UnmarshalSubConfig(cfg, srv.Name(), &serverCfg); err != nil {
return nil, fmt.Errorf("failed to unmarshal config: %w", err)
kocubinski marked this conversation as resolved.
Show resolved Hide resolved
}
}
methodsMap := appI.QueryHandlers()

grpcSrv := grpc.NewServer(
grpc.ForceServerCodec(newProtoCodec(appI.InterfaceRegistry()).GRPCCodec()),
grpc.ForceServerCodec(newProtoCodec(interfaceRegistry).GRPCCodec()),
grpc.MaxSendMsgSize(serverCfg.MaxSendMsgSize),
grpc.MaxRecvMsgSize(serverCfg.MaxRecvMsgSize),
grpc.UnknownServiceHandler(
makeUnknownServiceHandler(methodsMap, appI),
),
grpc.UnknownServiceHandler(makeUnknownServiceHandler(queryHandlers, queryable)),
)

// Reflection allows external clients to see what services and methods the gRPC server exposes.
gogoreflection.Register(grpcSrv, slices.Collect(maps.Keys(methodsMap)), logger.With("sub-module", "grpc-reflection"))
gogoreflection.Register(grpcSrv, slices.Collect(maps.Keys(queryHandlers)), logger.With("sub-module", "grpc-reflection"))

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

return srv, nil
}

s.grpcSrv = grpcSrv
s.config = serverCfg
s.logger = logger.With(log.ModuleKey, s.Name())
// 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], cfg map[string]any, logger log.Logger) error {

return nil
}
Expand Down
37 changes: 23 additions & 14 deletions server/v2/api/rest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package rest

import (
"context"
"cosmossdk.io/core/server"
"cosmossdk.io/server/v2/appmanager"
"errors"
"fmt"
"net/http"
Expand All @@ -24,29 +26,36 @@ type Server[T transaction.Tx] struct {
cfgOptions []CfgOption
}

func New[T transaction.Tx](cfgOptions ...CfgOption) *Server[T] {
return &Server[T]{
func New[T transaction.Tx](
appManager appmanager.AppManager[T],
logger log.Logger,
cfg server.ConfigMap,
cfgOptions ...CfgOption,
) (*Server[T], error) {
srv := &Server[T]{
cfgOptions: cfgOptions,
logger: logger.With(log.ModuleKey, ServerName),
router: http.NewServeMux(),
}

srv.router.Handle("/", NewDefaultHandler(appManager))

serverCfg := srv.Config().(*Config)
if len(cfg) > 0 {
if err := serverv2.UnmarshalSubConfig(cfg, srv.Name(), &serverCfg); err != nil {
return nil, fmt.Errorf("failed to unmarshal config: %w", err)
}
}
srv.config = serverCfg

return srv, nil
}

func (s *Server[T]) Name() string {
return ServerName
}

func (s *Server[T]) Init(appI serverv2.AppI[T], cfg map[string]any, logger log.Logger) error {
s.logger = logger.With(log.ModuleKey, s.Name())

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)
}
}

s.router = http.NewServeMux()
s.router.Handle("/", NewDefaultHandler(appI))
s.config = serverCfg

return nil
}
Expand Down
35 changes: 18 additions & 17 deletions server/v2/api/telemetry/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package telemetry

import (
"context"
"cosmossdk.io/core/server"
"encoding/json"
"fmt"
"net/http"
Expand All @@ -27,8 +28,23 @@ type Server[T transaction.Tx] struct {
}

// New creates a new telemetry server.
func New[T transaction.Tx]() *Server[T] {
return &Server[T]{}
func New[T transaction.Tx](cfg server.ConfigMap, logger log.Logger) (*Server[T], error) {
srv := &Server[T]{}
serverCfg := srv.Config().(*Config)
if len(cfg) > 0 {
if err := serverv2.UnmarshalSubConfig(cfg, srv.Name(), &serverCfg); err != nil {
return nil, fmt.Errorf("failed to unmarshal config: %w", err)
}
}
srv.config = serverCfg
srv.logger = logger.With(log.ModuleKey, srv.Name())

metrics, err := NewMetrics(srv.config)
if err != nil {
return nil, fmt.Errorf("failed to initialize metrics: %w", err)
}
srv.metrics = metrics
return srv, nil
}

// Name returns the server name.
Expand All @@ -46,21 +62,6 @@ func (s *Server[T]) Config() any {

// Init implements serverv2.ServerComponent.
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)
}
}
s.config = serverCfg
s.logger = logger.With(log.ModuleKey, s.Name())

metrics, err := NewMetrics(s.config)
if err != nil {
return fmt.Errorf("failed to initialize metrics: %w", err)
}
s.metrics = metrics

return nil
}

Expand Down
1 change: 1 addition & 0 deletions server/v2/cometbft/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ replace (
cosmossdk.io/x/consensus => ../../../x/consensus
cosmossdk.io/x/staking => ../../../x/staking
cosmossdk.io/x/tx => ../../../x/tx
cosmossdk.io/core => ../../../core
github.com/cosmos/cosmos-sdk => ../../../
)

Expand Down
Loading
Loading