Skip to content

Commit

Permalink
feat(client/v2): add autocli run + simapp example (cosmos#13867)
Browse files Browse the repository at this point in the history
Co-authored-by: Julien Robert <[email protected]>
  • Loading branch information
aaronc and julienrbrt authored Dec 31, 2022
1 parent 389765f commit 8719d5f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
17 changes: 17 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (

autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1"
"cosmossdk.io/client/v2/autocli"
"cosmossdk.io/core/appmodule"
dbm "github.com/cosmos/cosmos-db"
"github.com/spf13/cast"
abci "github.com/tendermint/tendermint/abci/types"
Expand Down Expand Up @@ -610,6 +612,21 @@ func (app *SimApp) TxConfig() client.TxConfig {
return app.txConfig
}

// AutoCliOpts returns the autocli options for the app.
func (app *SimApp) AutoCliOpts() autocli.AppOptions {
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
}
}
}

return autocli.AppOptions{Modules: modules}
}

// DefaultGenesis returns a default genesis from the registered AppModuleBasic's.
func (a *SimApp) DefaultGenesis() map[string]json.RawMessage {
return ModuleBasics.DefaultGenesis(a.appCodec)
Expand Down
5 changes: 3 additions & 2 deletions app_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"cosmossdk.io/core/appconfig"
"google.golang.org/protobuf/types/known/durationpb"

"github.com/cosmos/cosmos-sdk/runtime"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
"github.com/cosmos/cosmos-sdk/x/authz"
Expand Down Expand Up @@ -62,7 +63,7 @@ var (
distrtypes.ModuleName, stakingtypes.ModuleName, slashingtypes.ModuleName, govtypes.ModuleName,
minttypes.ModuleName, crisistypes.ModuleName, genutiltypes.ModuleName, evidencetypes.ModuleName, authz.ModuleName,
feegrant.ModuleName, nft.ModuleName, group.ModuleName, paramstypes.ModuleName, upgradetypes.ModuleName,
vestingtypes.ModuleName, consensustypes.ModuleName,
vestingtypes.ModuleName, consensustypes.ModuleName, runtime.ModuleName,
}

// module account permissions
Expand Down Expand Up @@ -92,7 +93,7 @@ var (
AppConfig = appconfig.Compose(&appv1alpha1.Config{
Modules: []*appv1alpha1.ModuleConfig{
{
Name: "runtime",
Name: runtime.ModuleName,
Config: appconfig.WrapAny(&runtimev1alpha1.Module{
AppName: "SimApp",
// During begin block slashing happens after distr.BeginBlocker so that
Expand Down
8 changes: 8 additions & 0 deletions app_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
dbm "github.com/cosmos/cosmos-db"
"github.com/tendermint/tendermint/libs/log"

"cosmossdk.io/client/v2/autocli"
"cosmossdk.io/depinject"

"github.com/cosmos/cosmos-sdk/baseapp"
Expand Down Expand Up @@ -121,6 +122,7 @@ type SimApp struct {
appCodec codec.Codec
txConfig client.TxConfig
interfaceRegistry codectypes.InterfaceRegistry
autoCliOpts autocli.AppOptions

// keepers
AccountKeeper authkeeper.AccountKeeper
Expand Down Expand Up @@ -221,6 +223,7 @@ func NewSimApp(
&app.legacyAmino,
&app.txConfig,
&app.interfaceRegistry,
&app.autoCliOpts,
&app.AccountKeeper,
&app.BankKeeper,
&app.CapabilityKeeper,
Expand Down Expand Up @@ -317,6 +320,11 @@ func (app *SimApp) TxConfig() client.TxConfig {
return app.txConfig
}

// AutoCliOpts returns the autocli options for the app.
func (app *SimApp) AutoCliOpts() autocli.AppOptions {
return app.autoCliOpts
}

// GetKey returns the KVStoreKey for the provided store key.
//
// NOTE: This is solely to be used for testing purposes.
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ go 1.19

require (
cosmossdk.io/api v0.2.6
cosmossdk.io/client/v2 v2.0.0-20221207142935-9bf027460bae
cosmossdk.io/core v0.4.0
cosmossdk.io/depinject v1.0.0-alpha.3
cosmossdk.io/math v1.0.0-beta.4
cosmossdk.io/tools/rosetta v0.2.0
github.com/cosmos/cosmos-db v0.0.0-20221226095112-f3c38ecb5e32
github.com/cosmos/cosmos-sdk v0.47.0-alpha2
github.com/cosmos/cosmos-sdk v0.47.0-alpha2.0.20221219231612-5ed4075ba219
github.com/golang/mock v1.6.0
github.com/spf13/cast v1.5.0
github.com/spf13/cobra v1.6.1
Expand Down Expand Up @@ -175,6 +176,8 @@ require (
)

replace (
// todo remove at next tag of client/v2
cosmossdk.io/client/v2 => ../client/v2
github.com/99designs/keyring => github.com/cosmos/keyring v1.2.0
// Simapp always use the latest version of the cosmos-sdk
github.com/cosmos/cosmos-sdk => ../.
Expand Down
18 changes: 17 additions & 1 deletion simd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ import (
// main function.
func NewRootCmd() *cobra.Command {
// we "pre"-instantiate the application for getting the injected/configured encoding configuration
tempApp := simapp.NewSimApp(log.NewNopLogger(), dbm.NewMemDB(), nil, true, simtestutil.NewAppOptionsWithFlagHome(simapp.DefaultNodeHome))
// note, this is not necessary when using app wiring, as depinject can be directly used.
// for consistency between app-v1 and app-v2, we do it the same way via methods on simapp
tempApp := simapp.NewSimApp(log.NewNopLogger(), dbm.NewMemDB(), nil, true, simtestutil.NewAppOptionsWithFlagHome(tempDir()))
encodingConfig := params.EncodingConfig{
InterfaceRegistry: tempApp.InterfaceRegistry(),
Codec: tempApp.AppCodec(),
Expand Down Expand Up @@ -88,6 +90,10 @@ func NewRootCmd() *cobra.Command {

initRootCmd(rootCmd, encodingConfig)

if err := tempApp.AutoCliOpts().EnhanceRootCommand(rootCmd); err != nil {
panic(err)
}

return rootCmd
}

Expand Down Expand Up @@ -307,3 +313,13 @@ func appExport(

return simApp.ExportAppStateAndValidators(forZeroHeight, jailAllowedAddrs, modulesToExport)
}

var tempDir = func() string {
dir, err := os.MkdirTemp("", "simapp")
if err != nil {
dir = simapp.DefaultNodeHome
}
defer os.RemoveAll(dir)

return dir
}

0 comments on commit 8719d5f

Please sign in to comment.