Skip to content

Commit

Permalink
refactor: improve x/upgrade app wiring (part of upgrade audit) (cos…
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt authored Dec 14, 2022
1 parent 32a143d commit 4df32b1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 28 deletions.
13 changes: 6 additions & 7 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package simapp

import (
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -184,7 +183,7 @@ type SimApp struct {
DistrKeeper distrkeeper.Keeper
GovKeeper govkeeper.Keeper
CrisisKeeper *crisiskeeper.Keeper
UpgradeKeeper upgradekeeper.Keeper
UpgradeKeeper *upgradekeeper.Keeper
ParamsKeeper paramskeeper.Keeper
AuthzKeeper authzkeeper.Keeper
EvidenceKeeper evidencekeeper.Keeper
Expand Down Expand Up @@ -265,7 +264,7 @@ func NewSimApp(

// load state streaming if enabled
if _, _, err := streaming.LoadStreamingServices(bApp, appOpts, appCodec, logger, keys); err != nil {
fmt.Printf("failed to load state streaming: %s", err)
logger.Error("failed to load state streaming", "err", err)
os.Exit(1)
}

Expand Down Expand Up @@ -360,15 +359,15 @@ func NewSimApp(
app.StakingKeeper, app.MsgServiceRouter(), govConfig, authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)

// Set legacy router for backwards compatibility with gov v1beta1
govKeeper.SetLegacyRouter(govRouter)

app.GovKeeper = *govKeeper.SetHooks(
govtypes.NewMultiGovHooks(
// register the governance hooks
),
)

// Set legacy router for backwards compatibility with gov v1beta1
govKeeper.SetLegacyRouter(govRouter)

app.NFTKeeper = nftkeeper.NewKeeper(keys[nftkeeper.StoreKey], appCodec, app.AccountKeeper, app.BankKeeper)

// create evidence keeper with router
Expand Down Expand Up @@ -514,7 +513,7 @@ func NewSimApp(

if loadLatest {
if err := app.LoadLatestVersion(); err != nil {
fmt.Println(err.Error())
logger.Error("error on loading last version", "err", err)
os.Exit(1)
}
}
Expand Down
2 changes: 1 addition & 1 deletion app_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ var (
KvStoreKey: "acc",
},
},
// InitGenesis: genesisModuleOrder,
InitGenesis: genesisModuleOrder,
// When ExportGenesis is not specified, the export genesis module order
// is equal to the init genesis order
// ExportGenesis: genesisModuleOrder,
Expand Down
31 changes: 12 additions & 19 deletions app_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ package simapp

import (
_ "embed"
"fmt"
"io"
"os"
"path/filepath"

abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
dbm "github.com/tendermint/tm-db"

Expand All @@ -27,7 +25,6 @@ import (
"github.com/cosmos/cosmos-sdk/store/streaming"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/testutil/testdata_pulsar"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/auth"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
Expand Down Expand Up @@ -136,7 +133,7 @@ type SimApp struct {
DistrKeeper distrkeeper.Keeper
GovKeeper *govkeeper.Keeper
CrisisKeeper *crisiskeeper.Keeper
UpgradeKeeper upgradekeeper.Keeper
UpgradeKeeper *upgradekeeper.Keeper
ParamsKeeper paramskeeper.Keeper
AuthzKeeper authzkeeper.Keeper
EvidenceKeeper evidencekeeper.Keeper
Expand Down Expand Up @@ -250,18 +247,12 @@ func NewSimApp(

// load state streaming if enabled
if _, _, err := streaming.LoadStreamingServices(app.App.BaseApp, appOpts, app.appCodec, logger, app.kvStoreKeys()); err != nil {
fmt.Printf("failed to load state streaming: %s", err)
logger.Error("failed to load state streaming", "err", err)
os.Exit(1)
}

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

// Set upgrade module options
app.UpgradeKeeper.SetVersionSetter(app.BaseApp)

app.ModuleManager.SetOrderInitGenesis(genesisModuleOrder...)
app.ModuleManager.SetOrderExportGenesis(genesisModuleOrder...)

app.ModuleManager.RegisterInvariants(app.CrisisKeeper)

// RegisterUpgradeHandlers is used for registering any on-chain upgrades.
Expand All @@ -281,8 +272,16 @@ func NewSimApp(

app.sm.RegisterStoreDecoders()

// initialize BaseApp
app.SetInitChainer(app.InitChainer)
// A custom InitChainer can be set if extra pre-init-genesis logic is required.
// By default, when using app wiring enabled module, this is not required.
// For instance, the upgrade module will set automatically the module version map in its init genesis thanks to app wiring.
// However, when registering a module manually (i.e. that does not support app wiring), the module version map
// must be set manually as follow. The upgrade module will de-duplicate the module version map.
//
// app.SetInitChainer(func(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
// app.UpgradeKeeper.SetModuleVersionMap(ctx, app.ModuleManager.GetVersionMap())
// return app.App.InitChainer(ctx, req)
// })

if err := app.Load(loadLatest); err != nil {
panic(err)
Expand All @@ -294,12 +293,6 @@ func NewSimApp(
// Name returns the name of the App
func (app *SimApp) Name() string { return app.BaseApp.Name() }

// InitChainer application update at chain initialization
func (app *SimApp) InitChainer(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
app.UpgradeKeeper.SetModuleVersionMap(ctx, app.ModuleManager.GetVersionMap())
return app.App.InitChainer(ctx, req)
}

// LegacyAmino returns SimApp's amino codec.
//
// NOTE: This is solely to be used for testing purposes as it may be desirable
Expand Down
1 change: 0 additions & 1 deletion state.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ func AppStateRandomizedFn(
accs []simtypes.Account, genesisTimestamp time.Time, appParams simtypes.AppParams,
) (json.RawMessage, []simtypes.Account) {
numAccs := int64(len(accs))
// TODO - in case runtime.RegisterModules(...) is used, the genesis state of the module won't be reflected here
genesisState := ModuleBasics.DefaultGenesis(cdc)

// generate a random amount of initial stake coins and a random initial
Expand Down

0 comments on commit 4df32b1

Please sign in to comment.