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(core,server): make commands not rely on server context #20422

Merged
merged 39 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
7a6f8aa
simplify server ctx
hieuvubk May 7, 2024
620509e
remove toolchain
hieuvubk May 7, 2024
67e13d1
fix build simapp
hieuvubk May 7, 2024
16c293b
fix
hieuvubk May 7, 2024
217c539
Merge branch 'main' into hieu/simplify_server_ctx
hieuvubk May 7, 2024
c46359d
fix init test
hieuvubk May 8, 2024
251fa0c
Merge branch 'hieu/simplify_server_ctx' of https://github.com/decentr…
hieuvubk May 8, 2024
95b2c29
fix export tests
hieuvubk May 8, 2024
467428e
network track config
hieuvubk May 8, 2024
b9a7278
fix gen test
hieuvubk May 8, 2024
4dcf9e6
Merge branch 'main' into hieu/simplify_server_ctx
hieuvubk May 8, 2024
e8d9242
fix gen_acc test
hieuvubk May 8, 2024
5e1df8d
Merge branch 'hieu/simplify_server_ctx' of https://github.com/decentr…
hieuvubk May 8, 2024
77c3bc1
fix e2e err
hieuvubk May 8, 2024
081cc22
Merge branch 'main' into hieu/simplify_server_ctx
hieuvubk May 8, 2024
0b8808e
rename
hieuvubk May 8, 2024
6c9992d
clean up
hieuvubk May 8, 2024
f63a54e
fix test
hieuvubk May 8, 2024
ef68e88
no more server context
hieuvubk May 11, 2024
eafb6bb
Merge branch 'main' into hieu/simplify_server_ctx
tac0turtle May 17, 2024
cc3a5df
fix network tests
hieuvubk May 20, 2024
e7aee2d
Merge branch 'main' into hieu/simplify_server_ctx
tac0turtle May 20, 2024
a360be7
lint + some cleanup
hieuvubk May 20, 2024
1d3c29f
Merge branch 'hieu/simplify_server_ctx' of https://github.com/cosmos/…
hieuvubk May 20, 2024
c27b303
err check
hieuvubk May 20, 2024
0bcea5e
lint
hieuvubk May 20, 2024
6ab5b56
rerun CI
hieuvubk May 20, 2024
b31896d
refactor: keep LegacyServerContext
hieuvubk May 22, 2024
7c4e602
address comment
hieuvubk May 22, 2024
baae5d4
key as type
hieuvubk May 22, 2024
5de4f6c
add deprecated cmt
hieuvubk May 23, 2024
9fbb8e8
revert: server v0 use Context
hieuvubk May 23, 2024
731751b
fix err format
hieuvubk May 23, 2024
d7e7864
lint
hieuvubk May 23, 2024
c90c6ac
add changelog
hieuvubk May 23, 2024
e3c0a03
rename
hieuvubk May 23, 2024
7896c75
unused
hieuvubk May 23, 2024
9fb1cf8
Merge branch 'main' into hieu/simplify_server_ctx
julienrbrt May 27, 2024
8eb2ebf
resolve conflict
hieuvubk May 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
44 changes: 44 additions & 0 deletions client/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/tls"
"errors"
"fmt"
"os"
"slices"
"strings"

Expand All @@ -16,9 +17,13 @@ import (

signingv1beta1 "cosmossdk.io/api/cosmos/tx/signing/v1beta1"

corectx "cosmossdk.io/core/context"
"cosmossdk.io/log"
cmtcfg "github.com/cometbft/cometbft/config"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/viper"
)

// ClientContextKey defines the context key used to retrieve a client.Context from
Expand Down Expand Up @@ -373,3 +378,42 @@ func SetCmdClientContext(cmd *cobra.Command, clientCtx Context) error {

return nil
}

hieuvubk marked this conversation as resolved.
Show resolved Hide resolved
func GetViperFromCmd(cmd *cobra.Command) *viper.Viper {
value := cmd.Context().Value(corectx.ViperContextKey)
v, ok := value.(*viper.Viper)
if !ok {
return viper.New()
}
return v
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure proper error handling when retrieving Viper instance from command context.

The function GetViperFromCmd returns a new Viper instance if it cannot find one in the command context. This might lead to unexpected behavior if the context was supposed to already contain a specific configuration. Consider throwing an error or logging a warning when the Viper instance is not found.

- if !ok {
-     return viper.New()
- }
+ if !ok {
+     return nil, fmt.Errorf("viper instance not found in command context")
+ }

Committable suggestion was skipped due low confidence.


func GetConfigFromCmd(cmd *cobra.Command) *cmtcfg.Config {
v := cmd.Context().Value(corectx.ViperContextKey)
fmt.Println("viper", v)
viper, ok := v.(*viper.Viper)
if !ok {
fmt.Println("viper rong")
return cmtcfg.DefaultConfig()
}
return GetConfigFromViper(viper)
}
hieuvubk marked this conversation as resolved.
Show resolved Hide resolved

func GetLoggerFromCmd(cmd *cobra.Command) log.Logger {
v := cmd.Context().Value(corectx.LoggerContextKey)
logger, ok := v.(log.Logger)
if !ok {
return log.NewLogger(os.Stdout)
hieuvubk marked this conversation as resolved.
Show resolved Hide resolved
}
return logger
}

func GetConfigFromViper(v *viper.Viper) *cmtcfg.Config {
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved
hieuvubk marked this conversation as resolved.
Show resolved Hide resolved
conf := cmtcfg.DefaultConfig()
err := v.Unmarshal(conf)
rootDir := v.GetString(flags.FlagHome)
if err != nil {
return cmtcfg.DefaultConfig().SetRoot(rootDir)
}
return conf.SetRoot(rootDir)
Comment on lines +408 to +415
Copy link
Contributor

Choose a reason for hiding this comment

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

Optimize error handling in GetConfigFromViper.

The function GetConfigFromViper handles errors by returning a default configuration with the root directory set. However, it might be more appropriate to also log the error or throw it back to the caller to handle it, as swallowing the error might hide underlying issues.

- return cmtcfg.DefaultConfig().SetRoot(rootDir)
+ return nil, fmt.Errorf("failed to unmarshal configuration: %w", err)

}
5 changes: 3 additions & 2 deletions client/snapshot/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/spf13/cobra"

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

Expand All @@ -14,7 +15,7 @@ func DeleteSnapshotCmd() *cobra.Command {
Short: "Delete a local snapshot",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := server.GetServerContextFromCmd(cmd)
viper := client.GetViperFromCmd(cmd)

height, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
Expand All @@ -25,7 +26,7 @@ func DeleteSnapshotCmd() *cobra.Command {
return err
}

snapshotStore, err := server.GetSnapshotStore(ctx.Viper)
snapshotStore, err := server.GetSnapshotStore(viper)
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions client/snapshot/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/spf13/cobra"

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

Expand All @@ -21,8 +22,8 @@ func DumpArchiveCmd() *cobra.Command {
Short: "Dump the snapshot as portable archive format",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := server.GetServerContextFromCmd(cmd)
snapshotStore, err := server.GetSnapshotStore(ctx.Viper)
viper := client.GetViperFromCmd(cmd)
snapshotStore, err := server.GetSnapshotStore(viper)
if err != nil {
return err
}
Expand Down
10 changes: 6 additions & 4 deletions client/snapshot/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"cosmossdk.io/log"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/server"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
)
Expand All @@ -16,20 +17,21 @@ func ExportSnapshotCmd[T servertypes.Application](appCreator servertypes.AppCrea
Short: "Export app state to snapshot store",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := server.GetServerContextFromCmd(cmd)
cfg := client.GetConfigFromCmd(cmd)
viper := client.GetViperFromCmd(cmd)

height, err := cmd.Flags().GetInt64("height")
if err != nil {
return err
}

home := ctx.Config.RootDir
db, err := openDB(home, server.GetAppDBBackend(ctx.Viper))
home := cfg.RootDir
db, err := openDB(home, server.GetAppDBBackend(viper))
if err != nil {
return err
}
logger := log.NewLogger(cmd.OutOrStdout())
app := appCreator(logger, db, nil, ctx.Viper)
app := appCreator(logger, db, nil, viper)

if height == 0 {
height = app.CommitMultiStore().LastCommitID().Version
Expand Down
5 changes: 3 additions & 2 deletions client/snapshot/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/spf13/cobra"

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

Expand All @@ -13,8 +14,8 @@ var ListSnapshotsCmd = &cobra.Command{
Use: "list",
Short: "List local snapshots",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := server.GetServerContextFromCmd(cmd)
snapshotStore, err := server.GetSnapshotStore(ctx.Viper)
viper := client.GetViperFromCmd(cmd)
snapshotStore, err := server.GetSnapshotStore(viper)
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions client/snapshot/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

snapshottypes "cosmossdk.io/store/snapshots/types"

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

Expand All @@ -27,8 +28,8 @@ func LoadArchiveCmd() *cobra.Command {
Short: "Load a snapshot archive file (.tar.gz) into snapshot store",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := server.GetServerContextFromCmd(cmd)
snapshotStore, err := server.GetSnapshotStore(ctx.Viper)
viper := client.GetViperFromCmd(cmd)
snapshotStore, err := server.GetSnapshotStore(viper)
if err != nil {
return err
}
Expand Down
10 changes: 6 additions & 4 deletions client/snapshot/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"cosmossdk.io/log"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/server"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
)
Expand All @@ -21,7 +22,8 @@ func RestoreSnapshotCmd[T servertypes.Application](appCreator servertypes.AppCre
Long: "Restore app state from local snapshot",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := server.GetServerContextFromCmd(cmd)
cfg := client.GetConfigFromCmd(cmd)
viper := client.GetViperFromCmd(cmd)

height, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
Expand All @@ -32,13 +34,13 @@ func RestoreSnapshotCmd[T servertypes.Application](appCreator servertypes.AppCre
return err
}

home := ctx.Config.RootDir
db, err := openDB(home, server.GetAppDBBackend(ctx.Viper))
home := cfg.RootDir
db, err := openDB(home, server.GetAppDBBackend(viper))
if err != nil {
return err
}
logger := log.NewLogger(cmd.OutOrStdout())
app := appCreator(logger, db, nil, ctx.Viper)
app := appCreator(logger, db, nil, viper)

sm := app.SnapshotManager()
return sm.RestoreLocalSnapshot(height, uint32(format))
Expand Down
6 changes: 6 additions & 0 deletions core/context/server_context.go
hieuvubk marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package context

var (
LoggerContextKey = "server.logger"
ViperContextKey = "server.viper"
)
4 changes: 2 additions & 2 deletions core/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ require (
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/rs/zerolog v1.32.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
Expand Down
8 changes: 4 additions & 4 deletions core/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSV
github.com/cosmos/gogoproto v1.4.12 h1:vB6Lbe/rtnYGjQuFxkPiPYiCybqFT8QvLipDZP8JpFE=
github.com/cosmos/gogoproto v1.4.12/go.mod h1:LnZob1bXRdUoqMMtwYlcR3wjiElmlC+FkjaZRv1/eLY=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
Expand All @@ -25,8 +25,8 @@ github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
Expand Down
11 changes: 8 additions & 3 deletions server/cmd/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

import (
"context"
"os"

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

"cosmossdk.io/log"

corectx "cosmossdk.io/core/context"
"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 @@ -37,9 +41,10 @@
// 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, server.ServerContextKey, srvCtx)
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved
ctx = context.WithValue(ctx, corectx.LoggerContextKey, log.NewLogger(os.Stdout))

Check failure on line 46 in server/cmd/execute.go

View workflow job for this annotation

GitHub Actions / golangci-lint

SA1029: should not use built-in type string as key for value; define your own type to avoid collisions (staticcheck)
Copy link
Contributor

Choose a reason for hiding this comment

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

Context key should be a custom type, not a built-in type.

- ctx = context.WithValue(ctx, corectx.LoggerContextKey, log.NewLogger(os.Stdout))
+ type contextKey string
+ const loggerKey contextKey = "logger"
+ ctx = context.WithValue(ctx, loggerKey, log.NewLogger(os.Stdout))

Using built-in types as context keys can lead to collisions. Define a custom type to use as a key to avoid this issue.

ctx = context.WithValue(ctx, corectx.ViperContextKey, viper.New())

Check failure on line 47 in server/cmd/execute.go

View workflow job for this annotation

GitHub Actions / golangci-lint

SA1029: should not use built-in type string as key for value; define your own type to avoid collisions (staticcheck)
Copy link
Contributor

Choose a reason for hiding this comment

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

Context key should be a custom type, not a built-in type.

- ctx = context.WithValue(ctx, corectx.ViperContextKey, viper.New())
+ type contextKey string
+ const viperKey contextKey = "viper"
+ ctx = context.WithValue(ctx, viperKey, viper.New())

Similar to the previous comment, use a custom type for the context key to prevent potential collisions.


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.

Suggested change
ctx = context.WithValue(ctx, corectx.ViperContextKey, viper.New())
type contextKey string
const viperKey contextKey = "viper"
ctx = context.WithValue(ctx, viperKey, viper.New())

Copy link
Contributor

Choose a reason for hiding this comment

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

Use custom types for context keys to avoid potential collisions.

- ctx = context.WithValue(ctx, corectx.LoggerContextKey, log.NewLogger(os.Stdout))
- ctx = context.WithValue(ctx, corectx.ViperContextKey, viper.New())
+ type contextKey string
+ const loggerKey contextKey = "logger"
+ const viperKey contextKey = "viper"
+ ctx = context.WithValue(ctx, loggerKey, log.NewLogger(os.Stdout))
+ ctx = context.WithValue(ctx, viperKey, viper.New())

Using built-in types as context keys can lead to collisions. Define a custom type to use as a key to avoid this issue.


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.

Suggested change
ctx = context.WithValue(ctx, corectx.LoggerContextKey, log.NewLogger(os.Stdout))
ctx = context.WithValue(ctx, corectx.ViperContextKey, viper.New())
type contextKey string
const loggerKey contextKey = "logger"
const viperKey contextKey = "viper"
ctx = context.WithValue(ctx, loggerKey, log.NewLogger(os.Stdout))
ctx = context.WithValue(ctx, viperKey, viper.New())


return ctx
}
22 changes: 9 additions & 13 deletions server/cmt_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ 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 @@ -73,8 +72,7 @@ func ShowNodeIDCmd() *cobra.Command {
Use: "show-node-id",
Short: "Show this node's ID",
RunE: func(cmd *cobra.Command, args []string) error {
serverCtx := GetServerContextFromCmd(cmd)
cfg := serverCtx.Config
cfg := client.GetConfigFromCmd(cmd)

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

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

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

Expand Down Expand Up @@ -362,22 +358,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 {
serverCtx := GetServerContextFromCmd(cmd)
logger := log.NewLogger(cmd.OutOrStdout())
cfg := serverCtx.Config
cfg := client.GetConfigFromCmd(cmd)
logger := client.GetLoggerFromCmd(cmd)
viper := client.GetViperFromCmd(cmd)

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

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

Expand Down
11 changes: 7 additions & 4 deletions server/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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 @@ -25,15 +26,17 @@ restarting CometBFT the transactions in block n will be re-executed against the
application.
`,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := GetServerContextFromCmd(cmd)
config := client.GetConfigFromCmd(cmd)
logger := client.GetLoggerFromCmd(cmd)
viper := client.GetViperFromCmd(cmd)

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