Skip to content

Commit

Permalink
Optimize options struct
Browse files Browse the repository at this point in the history
  • Loading branch information
cyilong committed Jun 7, 2024
1 parent c81442b commit 196188e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
26 changes: 8 additions & 18 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,23 +195,13 @@ var (
}
// module accounts that are allowed to receive tokens
allowedReceivingModAcc = map[string]bool{}
// app options
appOptions = IritaAppOptions{}
)

// Verify app interface at compile time
var _ simapp.App = (*IritaApp)(nil)

var AddModule AddModuleFun

var AnteHandler AnteHandlerFun

var UpgradePlan RegisterUpgradePlanFun

type AddModuleFun func(app *IritaApp, mm *module.Manager, keys map[string]*sdk.KVStoreKey)

type AnteHandlerFun func(app *IritaApp, handlerOptions appante.HandlerOptions) sdk.AnteHandler

type RegisterUpgradePlanFun func(app *IritaApp, configurator module.Configurator, mm *module.Manager)

func init() {
userHomeDir, err := os.UserHomeDir()
if err != nil {
Expand Down Expand Up @@ -546,8 +536,8 @@ func NewIritaApp(
)

// extend Modules
if AddModule != nil {
AddModule(app, app.mm, app.keys)
if appOptions.addModule != nil {
appOptions.addModule(app, app.mm, app.keys)
}

app.mm.SetOrderMigrations(
Expand Down Expand Up @@ -630,8 +620,8 @@ func NewIritaApp(
// },
// func(ctx sdk.Context, plan sdkupgrade.Plan) {},
// )
if UpgradePlan != nil {
UpgradePlan(app, app.configurator, app.mm)
if appOptions.upgradePlan != nil {
appOptions.upgradePlan(app, app.configurator, app.mm)
}

// set peer filter by node ID
Expand Down Expand Up @@ -828,8 +818,8 @@ func (app *IritaApp) BuildAnteHandler(encodingConfig simappparams.EncodingConfig
EvmKeeper: app.EvmKeeper,
}

if AnteHandler != nil {
return AnteHandler(app, handlerOptions)
if appOptions.anteHandler != nil {
return appOptions.anteHandler(app, handlerOptions)
}
return appante.NewAnteHandler(handlerOptions)
}
Expand Down
29 changes: 29 additions & 0 deletions app/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package app

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"

appante "github.com/bianjieai/irita/app/ante"
)

type AddModuleFun func(app *IritaApp, mm *module.Manager, keys map[string]*sdk.KVStoreKey)
type AnteHandlerFun func(app *IritaApp, handlerOptions appante.HandlerOptions) sdk.AnteHandler
type RegisterUpgradePlanFun func(app *IritaApp, configurator module.Configurator, mm *module.Manager)

type IritaAppOptions struct {
addModule AddModuleFun
anteHandler AnteHandlerFun
upgradePlan RegisterUpgradePlanFun
}

func NewAppOptions(addModule AddModuleFun, anteHandler AnteHandlerFun, registerUpgradePlan RegisterUpgradePlanFun, modules ...module.AppModuleBasic) {
for _, moduleBasic := range modules {
ModuleBasics[moduleBasic.Name()] = moduleBasic
}
appOptions = IritaAppOptions{
addModule: addModule,
anteHandler: anteHandler,
upgradePlan: registerUpgradePlan,
}
}

0 comments on commit 196188e

Please sign in to comment.