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 8 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
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
86 changes: 50 additions & 36 deletions server/v2/cometbft/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package cometbft

import (
"context"
appmodulev2 "cosmossdk.io/core/appmodule/v2"
"cosmossdk.io/core/server"
"cosmossdk.io/server/v2/appmanager"
"cosmossdk.io/server/v2/cometbft/types"
"crypto/sha256"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -49,34 +53,38 @@ type CometBFTServer[T transaction.Tx] struct {
}

func New[T transaction.Tx](
logger log.Logger,
appName string,
store types.Store,
appManager *appmanager.AppManager[T],
queryHandlers map[string]appmodulev2.Handler,
txCodec transaction.Codec[T],
cfg server.ConfigMap,
serverOptions ServerOptions[T],
cfgOptions ...CfgOption,
) *CometBFTServer[T] {
return &CometBFTServer[T]{
) (*CometBFTServer[T], error) {
srv := &CometBFTServer[T]{
initTxCodec: txCodec,
serverOptions: serverOptions,
cfgOptions: cfgOptions,
}
}

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)
appTomlConfig := srv.Config().(*AppTomlConfig)
configTomlConfig := cmtcfg.DefaultConfig().SetRoot(home)
kocubinski marked this conversation as resolved.
Show resolved Hide resolved
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, srv.Name(), &appTomlConfig); err != nil {
return nil, 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)
return nil, fmt.Errorf("failed to unmarshal config: %w", err)
}
}

s.config = Config{
srv.config = Config{
ConfigTomlConfig: configTomlConfig,
AppTomlConfig: appTomlConfig,
}
Expand All @@ -96,42 +104,44 @@ func (s *CometBFTServer[T]) Init(appI serverv2.AppI[T], cfg map[string]any, logg
}
}

indexEvents := make(map[string]struct{}, len(s.config.AppTomlConfig.IndexEvents))
for _, e := range s.config.AppTomlConfig.IndexEvents {
indexEvents := make(map[string]struct{}, len(srv.config.AppTomlConfig.IndexEvents))
for _, e := range srv.config.AppTomlConfig.IndexEvents {
indexEvents[e] = struct{}{}
}
Comment on lines +109 to 112
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add nil check for AppTomlConfig.IndexEvents

The initialization of indexEvents assumes AppTomlConfig.IndexEvents is not nil. Add a nil check to prevent potential panic.

-indexEvents := make(map[string]struct{}, len(srv.config.AppTomlConfig.IndexEvents))
-for _, e := range srv.config.AppTomlConfig.IndexEvents {
+events := srv.config.AppTomlConfig.IndexEvents
+if events == nil {
+    events = []string{}
+}
+indexEvents := make(map[string]struct{}, len(events))
+for _, e := range events {
    indexEvents[e] = struct{}{}
}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
indexEvents := make(map[string]struct{}, len(srv.config.AppTomlConfig.IndexEvents))
for _, e := range srv.config.AppTomlConfig.IndexEvents {
indexEvents[e] = struct{}{}
}
events := srv.config.AppTomlConfig.IndexEvents
if events == nil {
events = []string{}
}
indexEvents := make(map[string]struct{}, len(events))
for _, e := range events {
indexEvents[e] = struct{}{}
}


s.logger = logger.With(log.ModuleKey, s.Name())
rs := appI.Store()
srv.logger = logger.With(log.ModuleKey, srv.Name())
consensus := NewConsensus(
s.logger,
appI.Name(),
appI,
appI.Close,
s.serverOptions.Mempool(cfg),
logger,
appName,
appManager,
nil,
srv.serverOptions.Mempool(cfg),
indexEvents,
appI.QueryHandlers(),
rs,
s.config,
s.initTxCodec,
queryHandlers,
store,
srv.config,
srv.initTxCodec,
chainID,
)
consensus.prepareProposalHandler = s.serverOptions.PrepareProposalHandler
consensus.processProposalHandler = s.serverOptions.ProcessProposalHandler
consensus.checkTxHandler = s.serverOptions.CheckTxHandler
consensus.verifyVoteExt = s.serverOptions.VerifyVoteExtensionHandler
consensus.extendVote = s.serverOptions.ExtendVoteHandler
consensus.addrPeerFilter = s.serverOptions.AddrPeerFilter
consensus.idPeerFilter = s.serverOptions.IdPeerFilter

ss := rs.GetStateStorage().(snapshots.StorageSnapshotter)
sc := rs.GetStateCommitment().(snapshots.CommitSnapshotter)

snapshotStore, err := GetSnapshotStore(s.config.ConfigTomlConfig.RootDir)
consensus.prepareProposalHandler = srv.serverOptions.PrepareProposalHandler
consensus.processProposalHandler = srv.serverOptions.ProcessProposalHandler
consensus.checkTxHandler = srv.serverOptions.CheckTxHandler
consensus.verifyVoteExt = srv.serverOptions.VerifyVoteExtensionHandler
consensus.extendVote = srv.serverOptions.ExtendVoteHandler
consensus.addrPeerFilter = srv.serverOptions.AddrPeerFilter
consensus.idPeerFilter = srv.serverOptions.IdPeerFilter

ss := store.GetStateStorage().(snapshots.StorageSnapshotter)
sc := store.GetStateCommitment().(snapshots.CommitSnapshotter)
kocubinski marked this conversation as resolved.
Show resolved Hide resolved

snapshotStore, err := GetSnapshotStore(srv.config.ConfigTomlConfig.RootDir)
if err != nil {
return err
return nil, err
}
consensus.snapshotManager = snapshots.NewManager(snapshotStore, s.serverOptions.SnapshotOptions(cfg), sc, ss, nil, s.logger)
consensus.snapshotManager = snapshots.NewManager(
snapshotStore, srv.serverOptions.SnapshotOptions(cfg), sc, ss, nil, logger)

srv.Consensus = consensus

// initialize the indexer
if indexerCfg := s.config.AppTomlConfig.Indexer; len(indexerCfg.Target) > 0 {
Expand All @@ -148,6 +158,10 @@ func (s *CometBFTServer[T]) Init(appI serverv2.AppI[T], cfg map[string]any, logg

s.Consensus = consensus

return srv, nil
}

func (s *CometBFTServer[T]) Init(appI serverv2.AppI[T], cfg map[string]any, logger log.Logger) error {
return nil
}

Expand Down
Loading
Loading