diff --git a/app/app.go b/app/app.go index 43b61371..477aaa4b 100644 --- a/app/app.go +++ b/app/app.go @@ -38,7 +38,6 @@ import ( "github.com/cosmos/cosmos-sdk/server/api" "github.com/cosmos/cosmos-sdk/server/config" servertypes "github.com/cosmos/cosmos-sdk/server/types" - "github.com/cosmos/cosmos-sdk/std" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/types/module" @@ -62,8 +61,6 @@ import ( // this line is used by starport scaffolding # stargate/app/moduleImport "github.com/initia-labs/initia/app/keepers" - "github.com/initia-labs/initia/app/params" - cryptocodec "github.com/initia-labs/initia/crypto/codec" initiatx "github.com/initia-labs/initia/tx" moveconfig "github.com/initia-labs/initia/x/move/config" movetypes "github.com/initia-labs/initia/x/move/types" @@ -150,11 +147,7 @@ func NewInitiaApp( logger.Info("mempool max txs", "max_txs", mempoolMaxTxs) logger.Info("query gas limit", "gas_limit", queryGasLimit) - encodingConfig := params.MakeEncodingConfig() - std.RegisterLegacyAminoCodec(encodingConfig.Amino) - std.RegisterInterfaces(encodingConfig.InterfaceRegistry) - cryptocodec.RegisterLegacyAminoCodec(encodingConfig.Amino) - cryptocodec.RegisterInterfaces(encodingConfig.InterfaceRegistry) + encodingConfig := MakeEncodingConfig() appCodec := encodingConfig.Codec legacyAmino := encodingConfig.Amino @@ -225,7 +218,7 @@ func NewInitiaApp( // non-dependant module elements, such as codec registration and genesis verification. // By default it is composed of all the module from the module manager. // Additionally, app module basics can be overwritten by passing them as argument. - app.BasicModuleManager = newBasicManagerFromManager(app) + app.BasicModuleManager = NewBasicManager() // NOTE: upgrade module is required to be prioritized app.ModuleManager.SetOrderPreBlockers( diff --git a/app/encoding.go b/app/encoding.go index 7f295361..40eda57c 100644 --- a/app/encoding.go +++ b/app/encoding.go @@ -3,71 +3,66 @@ package app import ( "cosmossdk.io/client/v2/autocli" "cosmossdk.io/core/appmodule" - "cosmossdk.io/log" - dbm "github.com/cosmos/cosmos-db" "github.com/cosmos/cosmos-sdk/client/flags" runtimeservices "github.com/cosmos/cosmos-sdk/runtime/services" + "github.com/cosmos/cosmos-sdk/std" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" authcodec "github.com/cosmos/cosmos-sdk/x/auth/codec" + autocliv1 "cosmossdk.io/api/cosmos/autocli/v1" "github.com/initia-labs/initia/app/params" - moveconfig "github.com/initia-labs/initia/x/move/config" - oracleconfig "github.com/skip-mev/connect/v2/oracle/config" + cryptocodec "github.com/initia-labs/initia/crypto/codec" ) -func newTempApp() *InitiaApp { - return NewInitiaApp( - log.NewNopLogger(), - dbm.NewMemDB(), - nil, - true, - moveconfig.DefaultMoveConfig(), - oracleconfig.NewDefaultAppConfig(), - EmptyAppOptions{}, - ) -} - func MakeEncodingConfig() params.EncodingConfig { - tempApp := newTempApp() - encodingConfig := params.EncodingConfig{ - InterfaceRegistry: tempApp.InterfaceRegistry(), - Codec: tempApp.AppCodec(), - TxConfig: tempApp.TxConfig(), - Amino: tempApp.LegacyAmino(), - } + encodingConfig := params.MakeEncodingConfig() + std.RegisterLegacyAminoCodec(encodingConfig.Amino) + std.RegisterInterfaces(encodingConfig.InterfaceRegistry) + cryptocodec.RegisterLegacyAminoCodec(encodingConfig.Amino) + cryptocodec.RegisterInterfaces(encodingConfig.InterfaceRegistry) + + basicManager := NewBasicManager() + basicManager.RegisterInterfaces(encodingConfig.InterfaceRegistry) + basicManager.RegisterLegacyAminoCodec(encodingConfig.Amino) return encodingConfig } -func AutoCliOpts() autocli.AppOptions { - tempApp := newTempApp() - modules := make(map[string]appmodule.AppModule, 0) - for _, m := range tempApp.ModuleManager.Modules { +func AutoCliOpts(encodingConfig params.EncodingConfig) autocli.AppOptions { + appModules := make(map[string]appmodule.AppModule, 0) + moduleOptions := make(map[string]interface{}, 0) + + modules := modulesForAutoCli(encodingConfig.Codec, encodingConfig.TxConfig, encodingConfig.InterfaceRegistry, authcodec.NewBech32Codec(sdk.GetConfig().GetBech32AccountAddrPrefix()), authcodec.NewBech32Codec(sdk.GetConfig().GetBech32ValidatorAddrPrefix())) + + for _, m := range modules { if moduleWithName, ok := m.(module.HasName); ok { moduleName := moduleWithName.Name() + if _, ok := m.(interface { + AutoCLIOptions() *autocliv1.ModuleOptions + }); ok { + moduleOptions[moduleName] = m + } else { + continue + } + if appModule, ok := moduleWithName.(appmodule.AppModule); ok { - modules[moduleName] = appModule + appModules[moduleName] = appModule } } } return autocli.AppOptions{ - Modules: modules, - ModuleOptions: runtimeservices.ExtractAutoCLIOptions(tempApp.ModuleManager.Modules), + Modules: appModules, + ModuleOptions: runtimeservices.ExtractAutoCLIOptions(moduleOptions), AddressCodec: authcodec.NewBech32Codec(sdk.GetConfig().GetBech32AccountAddrPrefix()), ValidatorAddressCodec: authcodec.NewBech32Codec(sdk.GetConfig().GetBech32ValidatorAddrPrefix()), ConsensusAddressCodec: authcodec.NewBech32Codec(sdk.GetConfig().GetBech32ConsensusAddrPrefix()), } } -func BasicManager() module.BasicManager { - tempApp := newTempApp() - return tempApp.BasicModuleManager -} - // EmptyAppOptions is a stub implementing AppOptions type EmptyAppOptions struct{} diff --git a/app/genesis.go b/app/genesis.go index f7d03338..c35b5733 100644 --- a/app/genesis.go +++ b/app/genesis.go @@ -41,7 +41,7 @@ type GenesisState map[string]json.RawMessage // NewDefaultGenesisState generates the default state for the application. func NewDefaultGenesisState(cdc codec.Codec, bondDenom string) GenesisState { - return GenesisState(BasicManager().DefaultGenesis(cdc)). + return GenesisState(NewBasicManager().DefaultGenesis(cdc)). ConfigureBondDenom(cdc, bondDenom). ConfigureICA(cdc). AddMarketData(cdc, cdc.InterfaceRegistry().SigningContext().AddressCodec()) diff --git a/app/modules.go b/app/modules.go index ed9c1c37..e210772e 100644 --- a/app/modules.go +++ b/app/modules.go @@ -1,6 +1,8 @@ package app import ( + "context" + "golang.org/x/exp/maps" evidencetypes "cosmossdk.io/x/evidence/types" @@ -8,7 +10,8 @@ import ( feegrantmodule "cosmossdk.io/x/feegrant/module" "cosmossdk.io/x/upgrade" upgradetypes "cosmossdk.io/x/upgrade/types" - + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/types/module" "github.com/cosmos/cosmos-sdk/x/auth" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" @@ -51,6 +54,7 @@ import ( // this line is used by starport scaffolding # stargate/app/moduleImport + cdctypes "github.com/cosmos/cosmos-sdk/codec/types" authzmodule "github.com/initia-labs/initia/x/authz/module" "github.com/initia-labs/initia/x/bank" distr "github.com/initia-labs/initia/x/distribution" @@ -88,6 +92,49 @@ import ( dynamicfee "github.com/initia-labs/initia/x/dynamic-fee" dynamicfeetypes "github.com/initia-labs/initia/x/dynamic-fee/types" + + accountkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" + capabilitykeeper "github.com/cosmos/ibc-go/modules/capability/keeper" + bankkeeper "github.com/initia-labs/initia/x/bank/keeper" + + feegrantkeeper "cosmossdk.io/x/feegrant/keeper" + + authzkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper" + consensusparamkeeper "github.com/cosmos/cosmos-sdk/x/consensus/keeper" + groupkeeper "github.com/cosmos/cosmos-sdk/x/group/keeper" + + ratelimitkeeper "github.com/cosmos/ibc-apps/modules/rate-limiting/v8/keeper" + ibcfeekeeper "github.com/cosmos/ibc-go/v8/modules/apps/29-fee/keeper" + ibctransferkeeper "github.com/cosmos/ibc-go/v8/modules/apps/transfer/keeper" + + ibcnfttransferkeeper "github.com/initia-labs/initia/x/ibc/nft-transfer/keeper" + ibcpermkeeper "github.com/initia-labs/initia/x/ibc/perm/keeper" + icaauthkeeper "github.com/initia-labs/initia/x/intertx/keeper" + + // this line is used by starport scaffolding # stargate/app/moduleImport + + distrkeeper "github.com/initia-labs/initia/x/distribution/keeper" + dynamicfeekeeper "github.com/initia-labs/initia/x/dynamic-fee/keeper" + evidencekeeper "github.com/initia-labs/initia/x/evidence/keeper" + ibchookskeeper "github.com/initia-labs/initia/x/ibc-hooks/keeper" + movekeeper "github.com/initia-labs/initia/x/move/keeper" + stakingkeeper "github.com/initia-labs/initia/x/mstaking/keeper" + rewardkeeper "github.com/initia-labs/initia/x/reward/keeper" + slashingkeeper "github.com/initia-labs/initia/x/slashing/keeper" + + // block-sdk dependencies + + auctionkeeper "github.com/skip-mev/block-sdk/v2/x/auction/keeper" + + // connect oracle dependencies + + oraclekeeper "github.com/skip-mev/connect/v2/x/oracle/keeper" + + ophostkeeper "github.com/initia-labs/OPinit/x/ophost/keeper" + + sdk "github.com/cosmos/cosmos-sdk/types" + + "cosmossdk.io/core/address" ) var maccPerms = map[string][]string{ @@ -157,19 +204,89 @@ func appModules( } } -// ModuleBasics defines the module BasicManager that is in charge of setting up basic, -// non-dependant module elements, such as codec registration -// and genesis verification. -func newBasicManagerFromManager(app *InitiaApp) module.BasicManager { - basicManager := module.NewBasicManagerFromManager( - app.ModuleManager, - map[string]module.AppModuleBasic{ - genutiltypes.ModuleName: genutil.NewAppModuleBasic(genutil.DefaultMessageValidator), - govtypes.ModuleName: gov.NewAppModuleBasic(app.appCodec), - }) - basicManager.RegisterLegacyAminoCodec(app.legacyAmino) - basicManager.RegisterInterfaces(app.interfaceRegistry) - return basicManager +// modulesForAutoCli returns a list of modules for auto-cli +func modulesForAutoCli(appCodec codec.Codec, txConfig client.TxConfig, interfaceRegistry cdctypes.InterfaceRegistry, ac, vc address.Codec) []module.AppModule { + return []module.AppModule{ + genutil.NewAppModule(nil, nil, nil, txConfig), + auth.NewAppModule(appCodec, accountkeeper.AccountKeeper{}, nil, nil), + bank.NewAppModule(appCodec, bankkeeper.BaseKeeper{}, accountkeeper.AccountKeeper{}), + capability.NewAppModule(appCodec, capabilitykeeper.Keeper{}, false), + crisis.NewAppModule(nil, false, nil), + feegrantmodule.NewAppModule(appCodec, mockAccountKeeper{addressCodec: ac}, nil, feegrantkeeper.Keeper{}, interfaceRegistry), + gov.NewAppModule(appCodec, nil, nil, nil), + reward.NewAppModule(appCodec, rewardkeeper.Keeper{}), + slashing.NewAppModule(appCodec, slashingkeeper.Keeper{}), + distr.NewAppModule(appCodec, distrkeeper.Keeper{}), + staking.NewAppModule(appCodec, stakingkeeper.Keeper{}), + upgrade.NewAppModule(nil, ac), + evidence.NewAppModule(evidencekeeper.Keeper{}), + authzmodule.NewAppModule(appCodec, authzkeeper.Keeper{}, interfaceRegistry), + groupmodule.NewAppModule(appCodec, groupkeeper.Keeper{}, mockAccountKeeper{addressCodec: ac}, nil, interfaceRegistry), + consensus.NewAppModule(appCodec, consensusparamkeeper.Keeper{}), + move.NewAppModule(appCodec, movekeeper.Keeper{}, vc, maps.Keys(maccPerms)), + auction.NewAppModule(appCodec, auctionkeeper.Keeper{}), + ophost.NewAppModule(appCodec, ophostkeeper.Keeper{}), + // connect modules + oracle.NewAppModule(appCodec, oraclekeeper.Keeper{}), + marketmap.NewAppModule(appCodec, nil), + // ibc modules + ibc.NewAppModule(nil), + ibctransfer.NewAppModule(ibctransferkeeper.Keeper{}), + ibcnfttransfer.NewAppModule(appCodec, ibcnfttransferkeeper.Keeper{}), + ica.NewAppModule(nil, nil), + icaauth.NewAppModule(appCodec, icaauthkeeper.Keeper{}), + ibcfee.NewAppModule(ibcfeekeeper.Keeper{}), + ibcperm.NewAppModule(appCodec, ibcpermkeeper.Keeper{}), + ibctm.NewAppModule(), + solomachine.NewAppModule(), + packetforward.NewAppModule(nil, nil), + ibchooks.NewAppModule(appCodec, ibchookskeeper.Keeper{}), + forwarding.NewAppModule(nil), + ratelimit.NewAppModule(appCodec, ratelimitkeeper.Keeper{}), + dynamicfee.NewAppModule(appCodec, dynamicfeekeeper.Keeper{}), + } +} + +func NewBasicManager() module.BasicManager { + return module.NewBasicManager( + genutil.AppModuleBasic{}, + auth.AppModuleBasic{}, + bank.AppModuleBasic{}, + capability.AppModuleBasic{}, + crisis.AppModuleBasic{}, + feegrantmodule.AppModuleBasic{}, + gov.AppModuleBasic{}, + reward.AppModuleBasic{}, + slashing.AppModuleBasic{}, + distr.AppModuleBasic{}, + staking.AppModuleBasic{}, + upgrade.AppModuleBasic{}, + evidence.AppModuleBasic{}, + authzmodule.AppModuleBasic{}, + groupmodule.AppModuleBasic{}, + consensus.AppModuleBasic{}, + move.AppModuleBasic{}, + auction.AppModuleBasic{}, + ophost.AppModuleBasic{}, + // connect modules + oracle.AppModuleBasic{}, + marketmap.AppModuleBasic{}, + // ibc modules + ibc.AppModuleBasic{}, + ibctransfer.AppModuleBasic{}, + ibcnfttransfer.AppModuleBasic{}, + ica.AppModuleBasic{}, + icaauth.AppModuleBasic{}, + ibcfee.AppModuleBasic{}, + ibcperm.AppModuleBasic{}, + ibctm.AppModuleBasic{}, + solomachine.AppModuleBasic{}, + packetforward.AppModuleBasic{}, + ibchooks.AppModuleBasic{}, + forwarding.AppModuleBasic{}, + ratelimit.AppModuleBasic{}, + dynamicfee.AppModuleBasic{}, + ) } /* @@ -246,3 +363,27 @@ func orderInitBlockers() []string { forwardingtypes.ModuleName, ratelimittypes.ModuleName, } } + +// mockAccountKeeper is a mock implementation of the account keeper interface +// it is used to pass the address codec to the modules for auto-cli +type mockAccountKeeper struct { + addressCodec address.Codec +} + +func (ak mockAccountKeeper) AddressCodec() address.Codec { return ak.addressCodec } +func (ak mockAccountKeeper) NewAccount(ctx context.Context, acc sdk.AccountI) sdk.AccountI { + return nil +} +func (ak mockAccountKeeper) RemoveAccount(ctx context.Context, acc sdk.AccountI) {} +func (ak mockAccountKeeper) IterateAccounts(ctx context.Context, cb func(sdk.AccountI) bool) {} +func (ak mockAccountKeeper) GetAccount(ctx context.Context, addr sdk.AccAddress) sdk.AccountI { + return nil +} +func (ak mockAccountKeeper) GetModuleAccount(ctx context.Context, moduleName string) sdk.ModuleAccountI { + return nil +} +func (ak mockAccountKeeper) GetModuleAddress(moduleName string) sdk.AccAddress { return nil } +func (ak mockAccountKeeper) NewAccountWithAddress(ctx context.Context, addr sdk.AccAddress) sdk.AccountI { + return nil +} +func (ak mockAccountKeeper) SetAccount(ctx context.Context, acc sdk.AccountI) {} diff --git a/cmd/initiad/root.go b/cmd/initiad/root.go index e178256c..92f3648f 100644 --- a/cmd/initiad/root.go +++ b/cmd/initiad/root.go @@ -67,7 +67,7 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) { sdkConfig.Seal() encodingConfig := initiaapp.MakeEncodingConfig() - basicManager := initiaapp.BasicManager() + basicManager := initiaapp.NewBasicManager() // Get the executable name and configure the viper instance so that environmental // variables are checked based off that name. The underscore character is used @@ -145,7 +145,7 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) { initRootCmd(rootCmd, encodingConfig, basicManager) // add keyring to autocli opts - autoCliOpts := initiaapp.AutoCliOpts() + autoCliOpts := initiaapp.AutoCliOpts(encodingConfig) initClientCtx, _ = config.ReadFromClientConfig(initClientCtx) autoCliOpts.ClientCtx = initClientCtx