Skip to content

Commit

Permalink
Merge branch 'main' into hoa/fix-lint
Browse files Browse the repository at this point in the history
  • Loading branch information
hoank101 authored Oct 14, 2024
2 parents 89506df + 0b43fcc commit 8df8a64
Show file tree
Hide file tree
Showing 20 changed files with 86 additions and 52 deletions.
3 changes: 3 additions & 0 deletions server/v2/cometbft/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type Consensus[T transaction.Tx] struct {
logger log.Logger
appName, version string
app *appmanager.AppManager[T]
appCloser func() error
txCodec transaction.Codec[T]
store types.Store
streaming streaming.Manager
Expand Down Expand Up @@ -77,6 +78,7 @@ func NewConsensus[T transaction.Tx](
logger log.Logger,
appName string,
app *appmanager.AppManager[T],
appCloser func() error,
mp mempool.Mempool[T],
indexedEvents map[string]struct{},
queryHandlersMap map[string]appmodulev2.Handler,
Expand All @@ -89,6 +91,7 @@ func NewConsensus[T transaction.Tx](
appName: appName,
version: getCometBFTServerVersion(),
app: app,
appCloser: appCloser,
cfg: cfg,
store: store,
logger: logger,
Expand Down
6 changes: 3 additions & 3 deletions server/v2/cometbft/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"cosmossdk.io/core/store"
"cosmossdk.io/core/transaction"
"cosmossdk.io/log"
am "cosmossdk.io/server/v2/appmanager"
"cosmossdk.io/server/v2/appmanager"
"cosmossdk.io/server/v2/cometbft/handlers"
cometmock "cosmossdk.io/server/v2/cometbft/internal/mock"
"cosmossdk.io/server/v2/cometbft/mempool"
Expand Down Expand Up @@ -672,7 +672,7 @@ func setUpConsensus(t *testing.T, gasLimit uint64, mempool mempool.Mempool[mock.
sc := cometmock.NewMockCommiter(log.NewNopLogger(), string(actorName), "stf")
mockStore := cometmock.NewMockStore(ss, sc)

b := am.Builder[mock.Tx]{
b := appmanager.Builder[mock.Tx]{
STF: s,
DB: mockStore,
ValidateTxGasLimit: gasLimit,
Expand All @@ -688,7 +688,7 @@ func setUpConsensus(t *testing.T, gasLimit uint64, mempool mempool.Mempool[mock.
am, err := b.Build()
require.NoError(t, err)

return NewConsensus[mock.Tx](log.NewNopLogger(), "testing-app", am, mempool, map[string]struct{}{}, nil, mockStore, Config{AppTomlConfig: DefaultAppTomlConfig()}, mock.TxCodec{}, "test")
return NewConsensus[mock.Tx](log.NewNopLogger(), "testing-app", am, func() error { return nil }, mempool, map[string]struct{}{}, nil, mockStore, Config{AppTomlConfig: DefaultAppTomlConfig()}, mock.TxCodec{}, "test")
}

// Check target version same with store's latest version
Expand Down
1 change: 1 addition & 0 deletions server/v2/cometbft/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func (s *CometBFTServer[T]) Init(appI serverv2.AppI[T], cfg map[string]any, logg
s.logger,
appI.Name(),
appI.GetAppManager(),
appI.Close,
s.serverOptions.Mempool(cfg),
indexEvents,
appI.GetQueryHandlers(),
Expand Down
8 changes: 3 additions & 5 deletions server/v2/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/spf13/viper"

"cosmossdk.io/core/transaction"
"cosmossdk.io/log"
)

// Execute executes the root command of an application.
Expand All @@ -37,15 +36,14 @@ func Execute(rootCmd *cobra.Command, envPrefix, defaultHome string) error {
func AddCommands[T transaction.Tx](
rootCmd *cobra.Command,
newApp AppCreator[T],
logger log.Logger,
globalServerCfg ServerConfig,
components ...ServerComponent[T],
) error {
if len(components) == 0 {
return errors.New("no components provided")
}

server := NewServer(logger, globalServerCfg, components...)
server := NewServer(globalServerCfg, components...)
originalPersistentPreRunE := rootCmd.PersistentPreRunE
rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
// set the default command outputs
Expand Down Expand Up @@ -170,12 +168,12 @@ func configHandle[T transaction.Tx](s *Server[T], cmd *cobra.Command) error {
return err
}

log, err := NewLogger(v, cmd.OutOrStdout())
logger, err := NewLogger(v, cmd.OutOrStdout())
if err != nil {
return err
}

return SetCmdServerContext(cmd, v, log)
return SetCmdServerContext(cmd, v, logger)
}

// findSubCommand finds a sub-command of the provided command whose Use
Expand Down
9 changes: 4 additions & 5 deletions server/v2/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,15 @@ var _ ServerComponent[transaction.Tx] = (*Server[transaction.Tx])(nil)

// Server is the top-level server component which contains all other server components.
type Server[T transaction.Tx] struct {
logger log.Logger
components []ServerComponent[T]
config ServerConfig
}

func NewServer[T transaction.Tx](
logger log.Logger,
config ServerConfig,
components ...ServerComponent[T],
) *Server[T] {
return &Server[T]{
logger: logger,
config: config,
components: components,
}
Expand All @@ -86,7 +83,8 @@ func (s *Server[T]) Name() string {

// Start starts all components concurrently.
func (s *Server[T]) Start(ctx context.Context) error {
s.logger.Info("starting servers...")
logger := GetLoggerFromContext(ctx)
logger.With(log.ModuleKey, s.Name()).Info("starting servers...")

g, ctx := errgroup.WithContext(ctx)
for _, mod := range s.components {
Expand All @@ -106,7 +104,8 @@ func (s *Server[T]) Start(ctx context.Context) error {

// Stop stops all components concurrently.
func (s *Server[T]) Stop(ctx context.Context) error {
s.logger.Info("stopping servers...")
logger := GetLoggerFromContext(ctx)
logger.With(log.ModuleKey, s.Name()).Info("stopping servers...")

g, ctx := errgroup.WithContext(ctx)
for _, mod := range s.components {
Expand Down
7 changes: 5 additions & 2 deletions server/v2/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ func TestServer(t *testing.T) {
cfg := v.AllSettings()

logger := log.NewLogger(os.Stdout)

ctx, err := serverv2.SetServerContext(context.Background(), v, logger)
require.NoError(t, err)

grpcServer := grpc.New[transaction.Tx]()
err = grpcServer.Init(&mockApp[transaction.Tx]{}, cfg, logger)
require.NoError(t, err)
Expand All @@ -76,7 +80,6 @@ func TestServer(t *testing.T) {
mockServer := &mockServer{name: "mock-server-1", ch: make(chan string, 100)}

server := serverv2.NewServer(
logger,
serverv2.DefaultServerConfig(),
grpcServer,
storeServer,
Expand All @@ -97,7 +100,7 @@ func TestServer(t *testing.T) {
require.Equal(t, v.GetString(grpcServer.Name()+".address"), grpc.DefaultConfig().Address)

// start empty
ctx, cancelFn := context.WithCancel(context.TODO())
ctx, cancelFn := context.WithCancel(ctx)
go func() {
// wait 5sec and cancel context
<-time.After(5 * time.Second)
Expand Down
2 changes: 1 addition & 1 deletion server/v2/testdata/app.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ minimum-gas-prices = '0stake'
app-db-backend = 'goleveldb'

[store.options]
# SState storage database type. Currently we support: "sqlite", "pebble" and "rocksdb"
# State storage database type. Currently we support: "sqlite", "pebble" and "rocksdb"
ss-type = 'sqlite'
# State commitment database type. Currently we support: "iavl" and "iavl-v2"
sc-type = 'iavl'
Expand Down
1 change: 1 addition & 0 deletions server/v2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ type AppI[T transaction.Tx] interface {
GetQueryHandlers() map[string]appmodulev2.Handler
GetStore() store.RootStore
GetSchemaDecoderResolver() decoding.DecoderResolver
Close() error
}
52 changes: 35 additions & 17 deletions server/v2/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,62 @@ import (
"cosmossdk.io/log"
)

// SetServerContext sets the logger and viper in the context.
// The server manager expects the logger and viper to be set in the context.
func SetServerContext(ctx context.Context, viper *viper.Viper, logger log.Logger) (context.Context, error) {
if ctx == nil {
ctx = context.Background()
}

ctx = context.WithValue(ctx, corectx.LoggerContextKey, logger)
ctx = context.WithValue(ctx, corectx.ViperContextKey, viper)
return ctx, nil
}

// SetCmdServerContext sets a command's Context value to the provided argument.
// The server manager expects the logger and viper to be set in the context.
// 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()
ctx, err := SetServerContext(cmd.Context(), viper, logger)
if err != nil {
return err
}

cmdCtx = context.WithValue(cmdCtx, corectx.LoggerContextKey, logger)
cmdCtx = context.WithValue(cmdCtx, corectx.ViperContextKey, viper)
cmd.SetContext(cmdCtx)

cmd.SetContext(ctx)
return nil
}

func GetViperFromCmd(cmd *cobra.Command) *viper.Viper {
value := cmd.Context().Value(corectx.ViperContextKey)
// GetViperFromContext returns the viper instance from the context.
func GetViperFromContext(ctx context.Context) *viper.Viper {
value := ctx.Value(corectx.ViperContextKey)
v, ok := value.(*viper.Viper)
if !ok {
panic(fmt.Sprintf("incorrect viper type %T: expected *viper.Viper. Have you forgot to set the viper in the command context?", value))
panic(fmt.Sprintf("incorrect viper type %T: expected *viper.Viper. Have you forgot to set the viper in the context?", value))
}
return v
}

func GetLoggerFromCmd(cmd *cobra.Command) log.Logger {
v := cmd.Context().Value(corectx.LoggerContextKey)
// GetViperFromCmd returns the viper instance from the command context.
func GetViperFromCmd(cmd *cobra.Command) *viper.Viper {
return GetViperFromContext(cmd.Context())
}

// GetLoggerFromContext returns the logger instance from the context.
func GetLoggerFromContext(ctx context.Context) log.Logger {
v := ctx.Value(corectx.LoggerContextKey)
logger, ok := v.(log.Logger)
if !ok {
panic(fmt.Sprintf("incorrect logger type %T: expected log.Logger. Have you forgot to set the logger in the command context?", v))
panic(fmt.Sprintf("incorrect logger type %T: expected log.Logger. Have you forgot to set the logger in the context?", v))
}

return logger
}

// GetLoggerFromCmd returns the logger instance from the command context.
func GetLoggerFromCmd(cmd *cobra.Command) log.Logger {
return GetLoggerFromContext(cmd.Context())
}

// ExternalIP https://stackoverflow.com/questions/23558425/how-do-i-get-the-local-ip-address-in-go
// TODO there must be a better way to get external IP
func ExternalIP() (string, error) {
ifaces, err := net.Interfaces()
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions simapp/v2/app_di.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,16 @@ func (app *SimApp[T]) TxConfig() client.TxConfig {
return app.txConfig
}

// GetStore returns the root store.
func (app *SimApp[T]) GetStore() store.RootStore {
return app.store
}

// Close overwrites the base Close method to close the stores.
func (app *SimApp[T]) Close() error {
if err := app.store.Close(); err != nil {
return err
}

return app.App.Close()
}
8 changes: 1 addition & 7 deletions simapp/v2/simdv2/cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@ func initRootCmd[T transaction.Tx](
NewTestnetCmd(moduleManager),
)

logger, err := serverv2.NewLogger(viper.New(), rootCmd.OutOrStdout())
if err != nil {
panic(fmt.Sprintf("failed to create logger: %v", err))
}

// add keybase, auxiliary RPC, query, genesis, and tx child commands
rootCmd.AddCommand(
genesisCommand(moduleManager),
Expand All @@ -70,10 +65,9 @@ func initRootCmd[T transaction.Tx](
)

// wire server commands
if err = serverv2.AddCommands(
if err := serverv2.AddCommands(
rootCmd,
newApp,
logger,
initServerConfig(),
cometbft.New(
&genericTxDecoder[T]{txConfig},
Expand Down
2 changes: 1 addition & 1 deletion simapp/v2/simdv2/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func initCometConfig() cometbft.CfgOption {
cfg := cmtcfg.DefaultConfig()

// display only warn logs by default except for p2p and state
cfg.LogLevel = "*:warn,p2p:info,state:info"
cfg.LogLevel = "*:warn,server:info,p2p:info,state:info"
// increase block timeout
cfg.Consensus.TimeoutCommit = 5 * time.Second
// overwrite default pprof listen address
Expand Down
3 changes: 1 addition & 2 deletions simapp/v2/simdv2/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/spf13/pflag"

"cosmossdk.io/core/transaction"
"cosmossdk.io/log"
"cosmossdk.io/math"
"cosmossdk.io/math/unsafe"
runtimev2 "cosmossdk.io/runtime/v2"
Expand Down Expand Up @@ -344,7 +343,7 @@ func initTestnetFiles[T transaction.Tx](
)
storeServer := store.New[T]()
grpcServer := grpc.New[T](grpc.OverwriteDefaultConfig(grpcConfig))
server := serverv2.NewServer[T](log.NewNopLogger(), serverCfg, cometServer, grpcServer, storeServer)
server := serverv2.NewServer[T](serverCfg, cometServer, grpcServer, storeServer)
err = server.WriteConfig(filepath.Join(nodeDir, "config"))
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions store/v2/commitment/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
)

// MetadataStore is a store for metadata related to the commitment store.
// It isn't metadata store role to close the underlying KVStore.
type MetadataStore struct {
kv corestore.KVStoreWithBatch
}
Expand Down
4 changes: 2 additions & 2 deletions store/v2/root/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const (

// Options are the options for creating a root store.
type Options struct {
SSType SSType `mapstructure:"ss-type" toml:"ss-type" comment:"SState storage database type. Currently we support: \"sqlite\", \"pebble\" and \"rocksdb\""`
SSType SSType `mapstructure:"ss-type" toml:"ss-type" comment:"State storage database type. Currently we support: \"sqlite\", \"pebble\" and \"rocksdb\""`
SCType SCType `mapstructure:"sc-type" toml:"sc-type" comment:"State commitment database type. Currently we support: \"iavl\" and \"iavl-v2\""`
SSPruningOption *store.PruningOption `mapstructure:"ss-pruning-option" toml:"ss-pruning-option" comment:"Pruning options for state storage"`
SCPruningOption *store.PruningOption `mapstructure:"sc-pruning-option" toml:"sc-pruning-option" comment:"Pruning options for state commitment"`
Expand Down Expand Up @@ -177,5 +177,5 @@ func CreateRootStore(opts *FactoryOptions) (store.RootStore, error) {
}

pm := pruning.NewManager(sc, ss, storeOpts.SCPruningOption, storeOpts.SSPruningOption)
return New(opts.Logger, ss, sc, pm, nil, nil)
return New(opts.SCRawDB, opts.Logger, ss, sc, pm, nil, nil)
}
2 changes: 1 addition & 1 deletion store/v2/root/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (s *MigrateStoreTestSuite) SetupTest() {
pm := pruning.NewManager(sc, ss, nil, nil)

// assume no storage store, simulate the migration process
s.rootStore, err = New(testLog, ss, orgSC, pm, migrationManager, nil)
s.rootStore, err = New(dbm.NewMemDB(), testLog, ss, orgSC, pm, migrationManager, nil)
s.Require().NoError(err)
}

Expand Down
Loading

0 comments on commit 8df8a64

Please sign in to comment.