Skip to content

Commit

Permalink
update upgrade init_genesis
Browse files Browse the repository at this point in the history
  • Loading branch information
sin3A committed Jul 25, 2024
1 parent a42b13b commit 8a6a833
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 50 deletions.
53 changes: 4 additions & 49 deletions modules/upgrade/depinject.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,21 @@ package upgrade
import (
"cosmossdk.io/core/appmodule"
"cosmossdk.io/depinject"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/server"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
store "github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/types/module"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
sdk "github.com/cosmos/cosmos-sdk/x/upgrade"
"github.com/spf13/cast"
modulev1 "iritamod.bianjie.ai/api/iritamod/upgrade/module/v1"
"iritamod.bianjie.ai/modules/upgrade/keeper"
"iritamod.bianjie.ai/modules/upgrade/types"
)

// App Wiring Setup
func init() {
appmodule.Register(&modulev1.Module{},
appmodule.Provide(ProvideModule),
appmodule.Invoke(PopulateVersionMap),
)
}

Expand All @@ -49,52 +41,15 @@ type UpgradeOutputs struct {
depinject.Out
UpgradeKeeper keeper.Keeper
Module appmodule.AppModule
GovHandler govv1beta1.HandlerRoute
BaseAppOption runtime.BaseAppOption
}

func ProvideModule(in UpgradeInputs) UpgradeOutputs {
//authority := authtypes.NewModuleAddress(govtypes.ModuleName)
//if in.Config.Authority != "" {
// authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority)
//}
//skipUpgradeHeights := make(map[int64]bool)
//keeper := keeper.NewKeeper(skipUpgradeHeights, in.Key, in.Cdc, cast.ToString(in.AppOpts.Get(flags.FlagHome)), nil, authority.String())
//m := NewAppModule(keeper)
//return UpgradeOutputs{UpgradeKeeper: keeper, Module: m}
var (
homePath string
skipUpgradeHeights = make(map[int64]bool)
)

if in.AppOpts != nil {
for _, h := range cast.ToIntSlice(in.AppOpts.Get(server.FlagUnsafeSkipUpgrades)) {
skipUpgradeHeights[int64(h)] = true
}

homePath = cast.ToString(in.AppOpts.Get(flags.FlagHome))
}

// default to governance authority if not provided
authority := authtypes.NewModuleAddress(govtypes.ModuleName)
if in.Config.Authority != "" {
authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority)
}

// set the governance module account as the authority for conducting upgrades
k := keeper.NewKeeper(skipUpgradeHeights, in.Key, in.Cdc, homePath, nil, authority.String())
baseappOpt := func(app *baseapp.BaseApp) {
k.SetVersionSetter(app)
}
m := NewAppModule(k)
gh := govv1beta1.HandlerRoute{RouteKey: types.RouterKey, Handler: sdk.NewSoftwareUpgradeProposalHandler(k.Keeper)}

return UpgradeOutputs{UpgradeKeeper: k, Module: m, GovHandler: gh, BaseAppOption: baseappOpt}
}
func PopulateVersionMap(upgradeKeeper *keeper.Keeper, modules map[string]appmodule.AppModule) {
if upgradeKeeper == nil {
return
}

upgradeKeeper.SetInitVersionMap(module.NewManagerFromMap(modules).GetVersionMap())
skipUpgradeHeights := make(map[int64]bool)
keeper := keeper.NewKeeper(skipUpgradeHeights, in.Key, in.Cdc, cast.ToString(in.AppOpts.Get(flags.FlagHome)), nil, authority.String())
m := NewAppModule(keeper)
return UpgradeOutputs{UpgradeKeeper: keeper, Module: m}
}
14 changes: 13 additions & 1 deletion modules/upgrade/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,19 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
}

// InitGenesis is ignored, no sense in serializing future upgrades
func (am AppModule) InitGenesis(_ sdk.Context, _ codec.JSONCodec, _ json.RawMessage) []abci.ValidatorUpdate {
func (am AppModule) InitGenesis(ctx sdk.Context, _ codec.JSONCodec, _ json.RawMessage) []abci.ValidatorUpdate {
// set version map automatically if available
if versionMap := am.keeper.GetInitVersionMap(); versionMap != nil {
// chains can still use a custom init chainer for setting the version map
// this means that we need to combine the manually wired modules version map with app wiring enabled modules version map
for name, version := range am.keeper.GetModuleVersionMap(ctx) {
if _, ok := versionMap[name]; !ok {
versionMap[name] = version
}
}

am.keeper.SetModuleVersionMap(ctx, versionMap)
}
return []abci.ValidatorUpdate{}
}

Expand Down

0 comments on commit 8a6a833

Please sign in to comment.