refactor(core,server): make commands not rely on server context (#20422)
Co-authored-by: Hieu Vu <72878483+hieuvubk@users.noreply.github.com> Co-authored-by: Julien Robert <julien@rbrt.fr>
This commit is contained in:
co-authored by
Hieu Vu
Julien Robert
parent
1b47dc91a3
commit
e6e1a853de
@@ -8,13 +8,17 @@ import (
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
cmtcfg "github.com/cometbft/cometbft/config"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/pflag"
|
||||
"github.com/spf13/viper"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
|
||||
signingv1beta1 "cosmossdk.io/api/cosmos/tx/signing/v1beta1"
|
||||
corectx "cosmossdk.io/core/context"
|
||||
"cosmossdk.io/log"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
||||
@@ -373,3 +377,40 @@ func SetCmdClientContext(cmd *cobra.Command, clientCtx Context) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func GetConfigFromCmd(cmd *cobra.Command) *cmtcfg.Config {
|
||||
v := cmd.Context().Value(corectx.ViperContextKey{})
|
||||
viper, ok := v.(*viper.Viper)
|
||||
if !ok {
|
||||
return cmtcfg.DefaultConfig()
|
||||
}
|
||||
return GetConfigFromViper(viper)
|
||||
}
|
||||
|
||||
func GetLoggerFromCmd(cmd *cobra.Command) log.Logger {
|
||||
v := cmd.Context().Value(corectx.LoggerContextKey{})
|
||||
logger, ok := v.(log.Logger)
|
||||
if !ok {
|
||||
return log.NewLogger(cmd.OutOrStdout())
|
||||
}
|
||||
return logger
|
||||
}
|
||||
|
||||
func GetConfigFromViper(v *viper.Viper) *cmtcfg.Config {
|
||||
conf := cmtcfg.DefaultConfig()
|
||||
err := v.Unmarshal(conf)
|
||||
rootDir := v.GetString(flags.FlagHome)
|
||||
if err != nil {
|
||||
return cmtcfg.DefaultConfig().SetRoot(rootDir)
|
||||
}
|
||||
return conf.SetRoot(rootDir)
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/server"
|
||||
)
|
||||
|
||||
@@ -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 {
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/server"
|
||||
)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
)
|
||||
@@ -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
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/server"
|
||||
)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
|
||||
snapshottypes "cosmossdk.io/store/snapshots/types"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/server"
|
||||
)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
)
|
||||
@@ -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 {
|
||||
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user