Skip to content

Commit

Permalink
refactor: continue to reorder initializations in app.go and simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
cgorenflo committed Nov 4, 2023
1 parent 19cb029 commit f28b1aa
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 72 deletions.
112 changes: 40 additions & 72 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/cosmos/cosmos-sdk/server/api"
"github.com/cosmos/cosmos-sdk/server/config"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
store "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/version"
Expand Down Expand Up @@ -141,11 +140,6 @@ var (
// DefaultNodeHome default home directories for the application daemon
DefaultNodeHome string

// ModuleBasics defines the module BasicManager is in charge of setting up basic,
// non-dependant module elements, such as codec registration
// and genesis verification. It is dynamically initialized by GetModuleBasics method.
ModuleBasics module.BasicManager

// WasmEnabled indicates whether wasm module is added to the app.
// "true" setting means it will be, otherwise it won't.
// This is configured during the build.
Expand Down Expand Up @@ -204,23 +198,17 @@ func NewAxelarApp(
baseAppOptions ...func(*bam.BaseApp),
) *AxelarApp {

appCodec := encodingConfig.Codec

keys := createStoreKeys()
tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey)

keepers := newKeeperCache()
setKeeper(keepers, initParamsKeeper(appCodec, encodingConfig.Amino, keys[paramstypes.StoreKey], tkeys[paramstypes.TStoreKey]))
setKeeper(keepers, initParamsKeeper(encodingConfig, keys[paramstypes.StoreKey], tkeys[paramstypes.TStoreKey]))

interfaceRegistry := encodingConfig.InterfaceRegistry
// BaseApp handles interactions with Tendermint through the ABCI protocol
bApp := bam.NewBaseApp(Name, logger, db, encodingConfig.TxConfig.TxDecoder(), baseAppOptions...)
bApp.SetCommitMultiStoreTracer(traceStore)
bApp.SetVersion(version.Version)
bApp.SetInterfaceRegistry(interfaceRegistry)
bApp.SetParamStore(getSubspace(keepers, bam.Paramspace))
bApp := initBaseApp(db, traceStore, encodingConfig, keepers, baseAppOptions, logger)

moduleAccountPermissions := initModuleAccountPermissions()
appCodec := encodingConfig.Codec

// add keepers
setKeeper(keepers, initAccountKeeper(appCodec, keys, keepers, moduleAccountPermissions))
Expand Down Expand Up @@ -252,23 +240,21 @@ func NewAxelarApp(
// add capability keeper and ScopeToModule for ibc module
memKeys := sdk.NewMemoryStoreKeys(capabilitytypes.MemStoreKey)
capabilityK := capabilitykeeper.NewKeeper(appCodec, keys[capabilitytypes.StoreKey], memKeys[capabilitytypes.MemStoreKey])
setKeeper(keepers, *capabilityK)

// grant capabilities for the ibc and ibc-transfer modules
// grant capabilities for the ibc, ibc-transfer and wasm modules
scopedIBCK := capabilityK.ScopeToModule(ibchost.ModuleName)
scopedTransferK := capabilityK.ScopeToModule(ibctransfertypes.ModuleName)
scopedWasmK := capabilityK.ScopeToModule(wasm.ModuleName)

capabilityK.Seal()
setKeeper(keepers, *capabilityK)
setKeeper(keepers, initIBCKeeper(appCodec, keys, keepers, scopedIBCK))
// Custom axelarnet/evm/nexus keepers
setKeeper(keepers, axelarnetKeeper.NewKeeper(
appCodec, keys[axelarnetTypes.StoreKey], getSubspace(keepers, axelarnetTypes.ModuleName), getKeeper[*ibckeeper.Keeper](keepers).ChannelKeeper, getKeeper[feegrantkeeper.Keeper](keepers),
))

setKeeper(keepers, evmKeeper.NewKeeper(
appCodec, keys[evmTypes.StoreKey], getKeeper[paramskeeper.Keeper](keepers),
))

setKeeper(keepers, evmKeeper.NewKeeper(appCodec, keys[evmTypes.StoreKey], getKeeper[paramskeeper.Keeper](keepers)))
setKeeper(keepers, initNexusKeeper(appCodec, keys, keepers))

// IBC Transfer Stack: SendPacket
Expand Down Expand Up @@ -341,7 +327,7 @@ func NewAxelarApp(
wasmConfig := mustReadWasmConfig(appOpts)

if IsWasmEnabled() {
scopedWasmK := capabilityK.ScopeToModule(wasm.ModuleName)

// The last arguments can contain custom message handlers, and custom query handlers,
// if we want to allow any custom callbacks
wasmOpts = append(wasmOpts, wasmkeeper.WithMessageHandlerDecorator(func(old wasmkeeper.Messenger) wasmkeeper.Messenger {
Expand Down Expand Up @@ -382,52 +368,28 @@ func NewAxelarApp(

setKeeper(keepers, initGovernanceKeeper(appCodec, keys, keepers))

upgradeInfo, err := getKeeper[upgradekeeper.Keeper](keepers).ReadUpgradeInfoFromDisk()
if err != nil {
panic(err)
}

if upgradeInfo.Name == upgradeName(bApp.Version()) && !getKeeper[upgradekeeper.Keeper](keepers).IsSkipHeight(upgradeInfo.Height) {
storeUpgrades := store.StoreUpgrades{}

if IsWasmEnabled() {
storeUpgrades.Added = append(storeUpgrades.Added, ibchookstypes.StoreKey)
storeUpgrades.Added = append(storeUpgrades.Added, wasm.ModuleName)
}

// configure store loader that checks if version == upgradeHeight and applies store upgrades
bApp.SetStoreLoader(upgradetypes.UpgradeStoreLoader(upgradeInfo.Height, &storeUpgrades))
}

/**** Module Options ****/

// NOTE: we may consider parsing `appOpts` inside module constructors. For the moment
// we prefer to be more strict in what arguments the modules expect.
var skipGenesisInvariants = cast.ToBool(appOpts.Get(crisis.FlagSkipGenesisInvariants))

crisisK := getKeeper[crisiskeeper.Keeper](keepers)

appModules := initAppModules(keepers, bApp, encodingConfig, appCodec, crisisK, skipGenesisInvariants, capabilityK, interfaceRegistry, axelarnetModule)
appModules := initAppModules(keepers, bApp, encodingConfig, appOpts, axelarnetModule)

mm = module.NewManager(appModules...)
mm.SetOrderMigrations(orderMigrations()...)
mm.SetOrderBeginBlockers(orderBeginBlockers()...)
mm.SetOrderEndBlockers(orderEndBlockers()...)
mm.SetOrderInitGenesis(orderModulesForGenesis()...)

crisisK := getKeeper[crisiskeeper.Keeper](keepers)
mm.RegisterInvariants(&crisisK)

// register all module routes and module queriers
mm.RegisterRoutes(bApp.Router(), bApp.QueryRouter(), encodingConfig.Amino)
configurator = module.NewConfigurator(appCodec, bApp.MsgServiceRouter(), bApp.GRPCQueryRouter())
mm.RegisterServices(configurator)

anteHandler := initAnteHandlers(encodingConfig, keys, keepers, interfaceRegistry, wasmConfig)

var app = &AxelarApp{
BaseApp: bApp,
appCodec: appCodec,
interfaceRegistry: interfaceRegistry,
interfaceRegistry: encodingConfig.InterfaceRegistry,
stakingKeeper: getKeeper[stakingkeeper.Keeper](keepers),
crisisKeeper: getKeeper[crisiskeeper.Keeper](keepers),
distrKeeper: getKeeper[distrkeeper.Keeper](keepers),
Expand All @@ -447,7 +409,7 @@ func NewAxelarApp(
app.SetBeginBlocker(app.BeginBlocker)
app.SetEndBlocker(app.EndBlocker)

app.SetAnteHandler(anteHandler)
app.SetAnteHandler(initAnteHandlers(encodingConfig, keys, keepers, wasmConfig))

if loadLatest {
if err := app.LoadLatestVersion(); err != nil {
Expand All @@ -473,7 +435,23 @@ func NewAxelarApp(
return app
}

func initAppModules(keepers *keeperCache, bApp *bam.BaseApp, encodingConfig axelarParams.EncodingConfig, appCodec codec.Codec, crisisK crisiskeeper.Keeper, skipGenesisInvariants bool, capabilityK *capabilitykeeper.Keeper, interfaceRegistry types.InterfaceRegistry, axelarnetModule axelarnet.AppModule) []module.AppModule {
func initBaseApp(db dbm.DB, traceStore io.Writer, encodingConfig axelarParams.EncodingConfig, keepers *keeperCache, baseAppOptions []func(*bam.BaseApp), logger log.Logger) *bam.BaseApp {
bApp := bam.NewBaseApp(Name, logger, db, encodingConfig.TxConfig.TxDecoder(), baseAppOptions...)
bApp.SetCommitMultiStoreTracer(traceStore)
bApp.SetVersion(version.Version)
bApp.SetInterfaceRegistry(encodingConfig.InterfaceRegistry)
bApp.SetParamStore(getSubspace(keepers, bam.Paramspace))
return bApp
}

func initAppModules(keepers *keeperCache, bApp *bam.BaseApp, encodingConfig axelarParams.EncodingConfig, appOpts servertypes.AppOptions, axelarnetModule axelarnet.AppModule) []module.AppModule {
// NOTE: we may consider parsing `appOpts` inside module constructors. For the moment
// we prefer to be more strict in what arguments the modules expect.
var skipGenesisInvariants = cast.ToBool(appOpts.Get(crisis.FlagSkipGenesisInvariants))

crisisK := getKeeper[crisiskeeper.Keeper](keepers)
appCodec := encodingConfig.Codec

appModules := []module.AppModule{
genutil.NewAppModule(getKeeper[authkeeper.AccountKeeper](keepers), getKeeper[stakingkeeper.Keeper](keepers), bApp.DeliverTx, encodingConfig.TxConfig),
auth.NewAppModule(appCodec, getKeeper[authkeeper.AccountKeeper](keepers), nil),
Expand All @@ -488,7 +466,7 @@ func initAppModules(keepers *keeperCache, bApp *bam.BaseApp, encodingConfig axel
upgrade.NewAppModule(getKeeper[upgradekeeper.Keeper](keepers)),
evidence.NewAppModule(getKeeper[evidencekeeper.Keeper](keepers)),
params.NewAppModule(getKeeper[paramskeeper.Keeper](keepers)),
capability.NewAppModule(appCodec, *capabilityK),
capability.NewAppModule(appCodec, getKeeper[capabilitykeeper.Keeper](keepers)),
}

// wasm module needs to be added in a specific order
Expand All @@ -505,7 +483,7 @@ func initAppModules(keepers *keeperCache, bApp *bam.BaseApp, encodingConfig axel
evidence.NewAppModule(getKeeper[evidencekeeper.Keeper](keepers)),
ibc.NewAppModule(getKeeper[*ibckeeper.Keeper](keepers)),
transfer.NewAppModule(getKeeper[ibctransferkeeper.Keeper](keepers)),
feegrantmodule.NewAppModule(appCodec, getKeeper[authkeeper.AccountKeeper](keepers), getKeeper[bankkeeper.BaseKeeper](keepers), getKeeper[feegrantkeeper.Keeper](keepers), interfaceRegistry),
feegrantmodule.NewAppModule(appCodec, getKeeper[authkeeper.AccountKeeper](keepers), getKeeper[bankkeeper.BaseKeeper](keepers), getKeeper[feegrantkeeper.Keeper](keepers), encodingConfig.InterfaceRegistry),

snapshot.NewAppModule(getKeeper[snapKeeper.Keeper](keepers)),
multisig.NewAppModule(getKeeper[multisigKeeper.Keeper](keepers), getKeeper[stakingkeeper.Keeper](keepers), getKeeper[slashingkeeper.Keeper](keepers), getKeeper[snapKeeper.Keeper](keepers), getKeeper[rewardKeeper.Keeper](keepers), getKeeper[nexusKeeper.Keeper](keepers)),
Expand All @@ -528,7 +506,7 @@ func mustReadWasmConfig(appOpts servertypes.AppOptions) wasmtypes.WasmConfig {
return wasmConfig
}

func initAnteHandlers(encodingConfig axelarParams.EncodingConfig, keys map[string]*sdk.KVStoreKey, keepers *keeperCache, interfaceRegistry types.InterfaceRegistry, wasmConfig wasmtypes.WasmConfig) sdk.AnteHandler {
func initAnteHandlers(encodingConfig axelarParams.EncodingConfig, keys map[string]*sdk.KVStoreKey, keepers *keeperCache, wasmConfig wasmtypes.WasmConfig) sdk.AnteHandler {
// The baseAnteHandler handles signature verification and transaction pre-processing
baseAnteHandler, err := authAnte.NewAnteHandler(
authAnte.HandlerOptions{
Expand Down Expand Up @@ -561,7 +539,7 @@ func initAnteHandlers(encodingConfig axelarParams.EncodingConfig, keys map[strin
ante.NewLogMsgDecorator(encodingConfig.Codec),
ante.NewCheckCommissionRate(getKeeper[stakingkeeper.Keeper](keepers)),
ante.NewUndelegateDecorator(getKeeper[multisigKeeper.Keeper](keepers), getKeeper[nexusKeeper.Keeper](keepers), getKeeper[snapKeeper.Keeper](keepers)),
ante.NewCheckRefundFeeDecorator(interfaceRegistry, getKeeper[authkeeper.AccountKeeper](keepers), getKeeper[stakingkeeper.Keeper](keepers), getKeeper[snapKeeper.Keeper](keepers), getKeeper[rewardKeeper.Keeper](keepers)),
ante.NewCheckRefundFeeDecorator(encodingConfig.InterfaceRegistry, getKeeper[authkeeper.AccountKeeper](keepers), getKeeper[stakingkeeper.Keeper](keepers), getKeeper[snapKeeper.Keeper](keepers), getKeeper[rewardKeeper.Keeper](keepers)),
ante.NewCheckProxy(getKeeper[snapKeeper.Keeper](keepers)),
ante.NewRestrictedTx(getKeeper[permissionKeeper.Keeper](keepers)),
ibcante.NewAnteDecorator(getKeeper[*ibckeeper.Keeper](keepers)),
Expand Down Expand Up @@ -791,8 +769,8 @@ func createStoreKeys() map[string]*sdk.KVStoreKey {
permissionTypes.StoreKey)
}

func initParamsKeeper(appCodec codec.Codec, legacyAmino *codec.LegacyAmino, key, tkey sdk.StoreKey) paramskeeper.Keeper {
paramsKeeper := paramskeeper.NewKeeper(appCodec, legacyAmino, key, tkey)
func initParamsKeeper(encodingConfig axelarParams.EncodingConfig, key, tkey sdk.StoreKey) paramskeeper.Keeper {
paramsKeeper := paramskeeper.NewKeeper(encodingConfig.Codec, encodingConfig.Amino, key, tkey)

paramsKeeper.Subspace(bam.Paramspace).WithKeyTable(paramskeeper.ConsensusParamsKeyTable())

Expand All @@ -807,7 +785,6 @@ func initParamsKeeper(appCodec codec.Codec, legacyAmino *codec.LegacyAmino, key,
paramsKeeper.Subspace(ibctransfertypes.ModuleName)
paramsKeeper.Subspace(ibchost.ModuleName)
paramsKeeper.Subspace(wasm.ModuleName)

paramsKeeper.Subspace(snapTypes.ModuleName)
paramsKeeper.Subspace(multisigTypes.ModuleName)
paramsKeeper.Subspace(tssTypes.ModuleName)
Expand Down Expand Up @@ -906,10 +883,6 @@ func (app *AxelarApp) RegisterTendermintService(clientCtx client.Context) {
// non-dependant module elements, such as codec registration and genesis verification.
// Initialization is dependent on whether wasm is enabled.
func GetModuleBasics() module.BasicManager {
if ModuleBasics != nil {
return ModuleBasics
}

var wasmProposals []govclient.ProposalHandler
if IsWasmEnabled() {
wasmProposals = wasmclient.ProposalHandlers
Expand Down Expand Up @@ -942,13 +915,6 @@ func GetModuleBasics() module.BasicManager {
upgrade.AppModuleBasic{},
evidence.AppModuleBasic{},
vesting.AppModuleBasic{},
}

if IsWasmEnabled() {
managers = append(managers, wasm.AppModuleBasic{}, ibchooks.AppModuleBasic{})
}

managers = append(managers,
ibc.AppModuleBasic{},
transfer.AppModuleBasic{},

Expand All @@ -961,11 +927,13 @@ func GetModuleBasics() module.BasicManager {
axelarnet.AppModuleBasic{},
reward.AppModuleBasic{},
permission.AppModuleBasic{},
)
}

ModuleBasics = module.NewBasicManager(managers...)
if IsWasmEnabled() {
managers = append(managers, wasm.AppModuleBasic{}, ibchooks.AppModuleBasic{})
}

return ModuleBasics
return module.NewBasicManager(managers...)
}

// IsWasmEnabled returns whether wasm is enabled
Expand Down
20 changes: 20 additions & 0 deletions app/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package app

import (
"fmt"
store "github.com/cosmos/cosmos-sdk/store/types"
ibchookstypes "github.com/osmosis-labs/osmosis/x/ibc-hooks/types"
"reflect"
"strings"

Expand Down Expand Up @@ -241,6 +243,24 @@ func initUpgradeKeeper(appCodec codec.Codec, keys map[string]*sdk.KVStoreKey, sk
return mm.RunMigrations(ctx, *configurator, fromVM)
},
)

upgradeInfo, err := upgradeK.ReadUpgradeInfoFromDisk()
if err != nil {
panic(err)
}

if upgradeInfo.Name == upgradeName(bApp.Version()) && !upgradeK.IsSkipHeight(upgradeInfo.Height) {
storeUpgrades := store.StoreUpgrades{}

if IsWasmEnabled() {
storeUpgrades.Added = append(storeUpgrades.Added, ibchookstypes.StoreKey)
storeUpgrades.Added = append(storeUpgrades.Added, wasm.ModuleName)
}

// configure store loader that checks if version == upgradeHeight and applies store upgrades
bApp.SetStoreLoader(upgradetypes.UpgradeStoreLoader(upgradeInfo.Height, &storeUpgrades))
}

return upgradeK
}

Expand Down

0 comments on commit f28b1aa

Please sign in to comment.