Skip to content

Commit

Permalink
[CORE-538] Integrate AutoCLI as part of Cosmos 0.47 -> 0.50 upgrade (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lcwik committed Jan 5, 2024
1 parent e47b841 commit 7e793b6
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 18 deletions.
83 changes: 66 additions & 17 deletions protocol/cmd/dydxprotocold/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package cmd

import (
storetypes "cosmossdk.io/store/types"
"errors"
"github.com/cosmos/cosmos-sdk/codec/address"
"io"
"os"
"path/filepath"

"cosmossdk.io/client/v2/autocli"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/log"
"cosmossdk.io/store"
"cosmossdk.io/store/snapshots"
snapshottypes "cosmossdk.io/store/snapshots/types"
storetypes "cosmossdk.io/store/types"
confixcmd "cosmossdk.io/tools/confix/cmd"
tmcli "github.com/cometbft/cometbft/libs/cli"
dbm "github.com/cosmos/cosmos-db"
"github.com/cosmos/cosmos-sdk/baseapp"
Expand All @@ -21,10 +23,15 @@ import (
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/client/rpc"
"github.com/cosmos/cosmos-sdk/codec/address"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
runtimeservices "github.com/cosmos/cosmos-sdk/runtime/services"
"github.com/cosmos/cosmos-sdk/server"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
sdktypes "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
authcodec "github.com/cosmos/cosmos-sdk/x/auth/codec"
"github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/crisis"
Expand All @@ -37,6 +44,7 @@ import (

"github.com/spf13/cast"
"github.com/spf13/cobra"
"github.com/spf13/viper"

// Unnamed import of statik for swagger UI support.
// Used in cosmos-sdk when registering the route for swagger docs.
Expand Down Expand Up @@ -138,6 +146,10 @@ func NewRootCmdWithInterceptors(

initRootCmd(rootCmd, option, encodingConfig, appInterceptor)

if err := autoCliOpts(encodingConfig, initClientCtx).EnhanceRootCommand(rootCmd); err != nil {
panic(err)
}

return rootCmd
}

Expand Down Expand Up @@ -170,17 +182,16 @@ func initRootCmd(
AddGenesisAccountCmd(dydxapp.DefaultNodeHome),
tmcli.NewCompletionCmd(rootCmd, true),
debug.Cmd(),
/** TODO(CORE-538): config.Cmd(), is this now handled by AutoCLI? */
confixcmd.ConfigCommand(),
)

a := appCreator{encodingConfig}
server.AddCommands(
rootCmd,
dydxapp.DefaultNodeHome,
func(logger log.Logger, db dbm.DB, writer io.Writer, options servertypes.AppOptions) servertypes.Application {
return appInterceptor(a.newApp(logger, db, writer, options))
return appInterceptor(newApp(logger, db, writer, options))
},
a.appExport,
appExport,
func(cmd *cobra.Command) {
addModuleInitFlags(cmd)

Expand All @@ -192,11 +203,52 @@ func initRootCmd(

// add keybase, auxiliary RPC, query, and tx child commands
rootCmd.AddCommand(
//TODO(CORE-538) rpc.StatusCommand(), is this now handled by AutoCLI?
server.StatusCommand(),
queryCommand(),
txCommand(),
keys.Commands(),
)

// TODO(CORE-852) add rosetta commands
// rootCmd.AddCommand(rosettaCmd.RosettaCommand(encodingConfig.InterfaceRegistry, encodingConfig.Codec))
}

// autoCliOpts returns options based upon the modules in the dYdX v4 app.
//
// Creates an instance of the application that is discarded to enumerate the modules.
func autoCliOpts(encodingCfg dydxapp.EncodingConfig, initClientCtx client.Context) autocli.AppOptions {
app := dydxapp.New(
log.NewNopLogger(),
dbm.NewMemDB(),
nil,
true,
viper.New(),
)

modules := make(map[string]appmodule.AppModule, 0)
for _, m := range app.ModuleManager.Modules {
if moduleWithName, ok := m.(module.HasName); ok {
moduleName := moduleWithName.Name()
if appModule, ok := moduleWithName.(appmodule.AppModule); ok {
modules[moduleName] = appModule
}
}
}

cliKR, err := keyring.NewAutoCLIKeyring(initClientCtx.Keyring)
if err != nil {
panic(err)
}

return autocli.AppOptions{
Modules: modules,
ModuleOptions: runtimeservices.ExtractAutoCLIOptions(app.ModuleManager.Modules),
AddressCodec: authcodec.NewBech32Codec(sdktypes.GetConfig().GetBech32AccountAddrPrefix()),
ValidatorAddressCodec: authcodec.NewBech32Codec(sdktypes.GetConfig().GetBech32ValidatorAddrPrefix()),
ConsensusAddressCodec: authcodec.NewBech32Codec(sdktypes.GetConfig().GetBech32ConsensusAddrPrefix()),
Keyring: cliKR,
ClientCtx: initClientCtx,
}
}

// addModuleInitFlags adds module specific init flags.
Expand All @@ -216,14 +268,14 @@ func queryCommand() *cobra.Command {
}

cmd.AddCommand(
//TODO(CORE-538): authcmd.GetAccountCmd(), Is this handled by AutoCLI?
rpc.ValidatorCommand(),
//TODO(CORE-538): //TODO rpc.BlockCommand(), Is this handled by AutoCLI?
server.QueryBlockCmd(),
authcmd.QueryTxsByEventsCmd(),
authcmd.QueryTxCmd(),
)

// TODO(CORE-538): Add AutoCLI https://github.com/cosmos/cosmos-sdk/blob/main/UPGRADING.md#autocli
// Module specific query sub-commands are added by AutoCLI

cmd.PersistentFlags().String(flags.FlagChainID, "", "The network chain ID")

return cmd
Expand Down Expand Up @@ -251,18 +303,15 @@ func txCommand() *cobra.Command {
authcmd.GetDecodeCommand(),
)

// TODO(CORE-538): Add AutoCLI https://github.com/cosmos/cosmos-sdk/blob/main/UPGRADING.md#autocli
// Module specific tx sub-commands are added by AutoCLI

cmd.PersistentFlags().String(flags.FlagChainID, "", "The network chain ID")

return cmd
}

type appCreator struct {
encCfg dydxapp.EncodingConfig
}

// newApp initializes and returns a new app.
func (a appCreator) newApp(
func newApp(
logger log.Logger,
db dbm.DB,
traceStore io.Writer,
Expand Down Expand Up @@ -335,7 +384,7 @@ func (a appCreator) newApp(
//
// Deprecated: this feature relies on the use of known unstable, legacy export functionality
// from cosmos.
func (a appCreator) appExport(
func appExport(
logger log.Logger,
db dbm.DB,
traceStore io.Writer,
Expand Down
6 changes: 5 additions & 1 deletion protocol/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ require (
)

require (
cosmossdk.io/client/v2 v2.0.0-beta.1
cosmossdk.io/core v0.11.0
cosmossdk.io/errors v1.0.0
cosmossdk.io/log v1.2.1
cosmossdk.io/store v1.0.0
cosmossdk.io/tools/confix v0.1.0
cosmossdk.io/x/evidence v0.1.0
cosmossdk.io/x/feegrant v0.1.0
cosmossdk.io/x/tx v0.12.0
Expand All @@ -60,6 +62,7 @@ require (
github.com/pelletier/go-toml v1.9.5
github.com/rs/zerolog v1.31.0
github.com/shopspring/decimal v1.3.1
github.com/spf13/viper v1.16.0
google.golang.org/genproto/googleapis/api v0.0.0-20231012201019-e917dd12ba7a
google.golang.org/protobuf v1.31.0
)
Expand Down Expand Up @@ -133,6 +136,8 @@ require (
github.com/cosmos/gogogateway v1.2.0 // indirect
github.com/cosmos/ics23/go v0.10.0 // indirect
github.com/cosmos/ledger-cosmos-go v0.13.3 // indirect
github.com/creachadair/atomicfile v0.3.1 // indirect
github.com/creachadair/tomledit v0.0.24 // indirect
github.com/curioswitch/go-reassign v0.2.0 // indirect
github.com/daixiang0/gci v0.11.0 // indirect
github.com/danieljoos/wincred v1.1.2 // indirect
Expand Down Expand Up @@ -326,7 +331,6 @@ require (
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/viper v1.16.0 // indirect
github.com/ssgreg/nlreturn/v2 v2.2.1 // indirect
github.com/stbenjam/no-sprintf-host-port v0.1.1 // indirect
github.com/stretchr/objx v0.5.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions protocol/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ cosmossdk.io/math v1.2.0 h1:8gudhTkkD3NxOP2YyyJIYYmt6dQ55ZfJkDOaxXpy7Ig=
cosmossdk.io/math v1.2.0/go.mod h1:l2Gnda87F0su8a/7FEKJfFdJrM0JZRXQaohlgJeyQh0=
cosmossdk.io/store v1.0.0 h1:6tnPgTpTSIskaTmw/4s5C9FARdgFflycIc9OX8i1tOI=
cosmossdk.io/store v1.0.0/go.mod h1:ABMprwjvx6IpMp8l06TwuMrj6694/QP5NIW+X6jaTYc=
cosmossdk.io/tools/confix v0.1.0 h1:2OOZTtQsDT5e7P3FM5xqM0bPfluAxZlAwxqaDmYBE+E=
cosmossdk.io/tools/confix v0.1.0/go.mod h1:TdXKVYs4gEayav5wM+JHT+kTU2J7fozFNqoVaN+8CdY=
cosmossdk.io/x/circuit v0.1.0 h1:IAej8aRYeuOMritczqTlljbUVHq1E85CpBqaCTwYgXs=
cosmossdk.io/x/circuit v0.1.0/go.mod h1:YDzblVE8+E+urPYQq5kq5foRY/IzhXovSYXb4nwd39w=
cosmossdk.io/x/evidence v0.1.0 h1:J6OEyDl1rbykksdGynzPKG5R/zm6TacwW2fbLTW4nCk=
Expand Down Expand Up @@ -467,6 +469,10 @@ github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwc
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creachadair/atomicfile v0.3.1 h1:yQORkHjSYySh/tv5th1dkKcn02NEW5JleB84sjt+W4Q=
github.com/creachadair/atomicfile v0.3.1/go.mod h1:mwfrkRxFKwpNAflYZzytbSwxvbK6fdGRRlp0KEQc0qU=
github.com/creachadair/tomledit v0.0.24 h1:5Xjr25R2esu1rKCbQEmjZYlrhFkDspoAbAKb6QKQDhQ=
github.com/creachadair/tomledit v0.0.24/go.mod h1:9qHbShRWQzSCcn617cMzg4eab1vbLCOjOshAWSzWr8U=
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY=
Expand Down

0 comments on commit 7e793b6

Please sign in to comment.