这是indexloc提供的服务,不要输入任何密码
Skip to content

extract creating encoding config from initia app #385

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 2 additions & 9 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
65 changes: 30 additions & 35 deletions app/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,71 +3,66 @@
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 {

Check warning on line 40 in app/encoding.go

View check run for this annotation

Codecov / codecov/patch

app/encoding.go#L34-L40

Added lines #L34 - L40 were not covered by tests
if moduleWithName, ok := m.(module.HasName); ok {
moduleName := moduleWithName.Name()
if _, ok := m.(interface {
AutoCLIOptions() *autocliv1.ModuleOptions
}); ok {
moduleOptions[moduleName] = m
} else {
continue

Check warning on line 48 in app/encoding.go

View check run for this annotation

Codecov / codecov/patch

app/encoding.go#L43-L48

Added lines #L43 - L48 were not covered by tests
}

if appModule, ok := moduleWithName.(appmodule.AppModule); ok {
modules[moduleName] = appModule
appModules[moduleName] = appModule

Check warning on line 52 in app/encoding.go

View check run for this annotation

Codecov / codecov/patch

app/encoding.go#L52

Added line #L52 was not covered by tests
}
}
}

return autocli.AppOptions{
Modules: modules,
ModuleOptions: runtimeservices.ExtractAutoCLIOptions(tempApp.ModuleManager.Modules),
Modules: appModules,
ModuleOptions: runtimeservices.ExtractAutoCLIOptions(moduleOptions),

Check warning on line 59 in app/encoding.go

View check run for this annotation

Codecov / codecov/patch

app/encoding.go#L58-L59

Added lines #L58 - L59 were not covered by tests
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{}

Expand Down
2 changes: 1 addition & 1 deletion app/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
169 changes: 155 additions & 14 deletions app/modules.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package app

import (
"context"

"golang.org/x/exp/maps"

evidencetypes "cosmossdk.io/x/evidence/types"
"cosmossdk.io/x/feegrant"
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"
Expand Down Expand Up @@ -51,6 +54,7 @@

// 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"
Expand Down Expand Up @@ -88,6 +92,49 @@

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{
Expand Down Expand Up @@ -157,19 +204,89 @@
}
}

// 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{}),
}

Check warning on line 247 in app/modules.go

View check run for this annotation

Codecov / codecov/patch

app/modules.go#L208-L247

Added lines #L208 - L247 were not covered by tests
}

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{},
)
}

/*
Expand Down Expand Up @@ -246,3 +363,27 @@
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

Check warning on line 375 in app/modules.go

View check run for this annotation

Codecov / codecov/patch

app/modules.go#L373-L375

Added lines #L373 - L375 were not covered by tests
}
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

Check warning on line 380 in app/modules.go

View check run for this annotation

Codecov / codecov/patch

app/modules.go#L377-L380

Added lines #L377 - L380 were not covered by tests
}
func (ak mockAccountKeeper) GetModuleAccount(ctx context.Context, moduleName string) sdk.ModuleAccountI {
return nil

Check warning on line 383 in app/modules.go

View check run for this annotation

Codecov / codecov/patch

app/modules.go#L382-L383

Added lines #L382 - L383 were not covered by tests
}
func (ak mockAccountKeeper) GetModuleAddress(moduleName string) sdk.AccAddress { return nil }
func (ak mockAccountKeeper) NewAccountWithAddress(ctx context.Context, addr sdk.AccAddress) sdk.AccountI {
return nil

Check warning on line 387 in app/modules.go

View check run for this annotation

Codecov / codecov/patch

app/modules.go#L385-L387

Added lines #L385 - L387 were not covered by tests
}
func (ak mockAccountKeeper) SetAccount(ctx context.Context, acc sdk.AccountI) {}

Check warning on line 389 in app/modules.go

View check run for this annotation

Codecov / codecov/patch

app/modules.go#L389

Added line #L389 was not covered by tests
4 changes: 2 additions & 2 deletions cmd/initiad/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
Loading