Skip to content

Commit

Permalink
revert: server v0 use Context
Browse files Browse the repository at this point in the history
  • Loading branch information
hieuvubk committed May 23, 2024
1 parent 5de4f6c commit 9fbb8e8
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 221 deletions.
11 changes: 3 additions & 8 deletions server/cmd/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@ package cmd

import (
"context"
"os"

cmtcli "github.com/cometbft/cometbft/libs/cli"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
"github.com/spf13/viper"

corectx "cosmossdk.io/core/context"
"cosmossdk.io/log"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/server"
)

// Execute executes the root command of an application. It handles creating a
Expand Down Expand Up @@ -41,10 +37,9 @@ func Execute(rootCmd *cobra.Command, envPrefix, defaultHome string) error {
// CreateExecuteContext returns a base Context with server and client context
// values initialized.
func CreateExecuteContext(ctx context.Context) context.Context {
// srvCtx := server.NewDefaultContext()
srvCtx := server.NewDefaultContext()
ctx = context.WithValue(ctx, client.ClientContextKey, &client.Context{})
ctx = context.WithValue(ctx, corectx.LoggerContextKey{}, log.NewLogger(os.Stdout))
ctx = context.WithValue(ctx, corectx.ViperContextKey{}, viper.New())
ctx = context.WithValue(ctx, server.ServerContextKey, srvCtx)

return ctx
}
22 changes: 13 additions & 9 deletions server/cmt_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/spf13/cobra"
"sigs.k8s.io/yaml"

"cosmossdk.io/log"
auth "cosmossdk.io/x/auth/client/cli"

"github.com/cosmos/cosmos-sdk/client"
Expand Down Expand Up @@ -72,7 +73,8 @@ func ShowNodeIDCmd() *cobra.Command {
Use: "show-node-id",
Short: "Show this node's ID",
RunE: func(cmd *cobra.Command, args []string) error {
cfg := client.GetConfigFromCmd(cmd)
serverCtx := GetServerContextFromCmd(cmd)
cfg := serverCtx.Config

nodeKey, err := p2p.LoadNodeKey(cfg.NodeKeyFile())
if err != nil {
Expand All @@ -91,7 +93,8 @@ func ShowValidatorCmd() *cobra.Command {
Use: "show-validator",
Short: "Show this node's CometBFT validator info",
RunE: func(cmd *cobra.Command, args []string) error {
cfg := client.GetConfigFromCmd(cmd)
serverCtx := GetServerContextFromCmd(cmd)
cfg := serverCtx.Config

privValidator := pvm.LoadFilePV(cfg.PrivValidatorKeyFile(), cfg.PrivValidatorStateFile())
pk, err := privValidator.GetPubKey()
Expand Down Expand Up @@ -124,7 +127,8 @@ func ShowAddressCmd() *cobra.Command {
Use: "show-address",
Short: "Shows this node's CometBFT validator consensus address",
RunE: func(cmd *cobra.Command, args []string) error {
cfg := client.GetConfigFromCmd(cmd)
serverCtx := GetServerContextFromCmd(cmd)
cfg := serverCtx.Config

privValidator := pvm.LoadFilePV(cfg.PrivValidatorKeyFile(), cfg.PrivValidatorStateFile())

Expand Down Expand Up @@ -358,22 +362,22 @@ func BootstrapStateCmd[T types.Application](appCreator types.AppCreator[T]) *cob
Short: "Bootstrap CometBFT state at an arbitrary block height using a light client",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
cfg := client.GetConfigFromCmd(cmd)
logger := client.GetLoggerFromCmd(cmd)
viper := client.GetViperFromCmd(cmd)
serverCtx := GetServerContextFromCmd(cmd)
logger := log.NewLogger(cmd.OutOrStdout())
cfg := serverCtx.Config

height, err := cmd.Flags().GetInt64("height")
if err != nil {
return err
}
if height == 0 {
home := viper.GetString(flags.FlagHome)
db, err := OpenDB(home, GetAppDBBackend(viper))
home := serverCtx.Viper.GetString(flags.FlagHome)
db, err := OpenDB(home, GetAppDBBackend(serverCtx.Viper))
if err != nil {
return err
}

app := appCreator(logger, db, nil, viper)
app := appCreator(logger, db, nil, serverCtx.Viper)
height = app.CommitMultiStore().LastCommitID().Version
}

Expand Down
11 changes: 4 additions & 7 deletions server/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
cmtcmd "github.com/cometbft/cometbft/cmd/cometbft/commands"
"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/server/types"
)

Expand All @@ -26,17 +25,15 @@ restarting CometBFT the transactions in block n will be re-executed against the
application.
`,
RunE: func(cmd *cobra.Command, args []string) error {
config := client.GetConfigFromCmd(cmd)
logger := client.GetLoggerFromCmd(cmd)
viper := client.GetViperFromCmd(cmd)
ctx := GetServerContextFromCmd(cmd)

db, err := OpenDB(config.RootDir, GetAppDBBackend(viper))
db, err := OpenDB(ctx.Config.RootDir, GetAppDBBackend(ctx.Viper))
if err != nil {
return err
}
app := appCreator(logger, db, nil, viper)
app := appCreator(ctx.Logger, db, nil, ctx.Viper)
// rollback CometBFT state
height, hash, err := cmtcmd.RollbackState(config, removeBlock)
height, hash, err := cmtcmd.RollbackState(ctx.Config, removeBlock)
if err != nil {
return fmt.Errorf("failed to rollback CometBFT state: %w", err)
}
Expand Down
Loading

0 comments on commit 9fbb8e8

Please sign in to comment.