Skip to content
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

chore: v0.3.2 upgrade handler #481

Merged
merged 4 commits into from
Jul 26, 2024
Merged
Changes from 3 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
82 changes: 78 additions & 4 deletions app/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ import (
"context"
"fmt"

"cosmossdk.io/store/prefix"
storetypes "cosmossdk.io/store/types"
circuittypes "cosmossdk.io/x/circuit/types"
upgradetypes "cosmossdk.io/x/upgrade/types"
tenderminttypes "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
consensustypes "github.com/cosmos/cosmos-sdk/x/consensus/types"
crisistypes "github.com/cosmos/cosmos-sdk/x/crisis/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper"
v1migrations "github.com/cosmos/cosmos-sdk/x/gov/migrations/v1"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
Expand Down Expand Up @@ -45,6 +47,7 @@ func (app App) RegisterUpgradeHandlers() {
app.registerUpgrade0_3_1Rc1(upgradeInfo)
app.registerUpgrade0_3_1Rc2(upgradeInfo)
app.registerUpgrade0_3_1(upgradeInfo)
app.registerUpgrade0_3_2(upgradeInfo)
app.registerUpgrade0_4_0(upgradeInfo)
}

Expand Down Expand Up @@ -208,15 +211,26 @@ func (app *App) registerUpgrade0_3_1Rc2(_ upgradetypes.Plan) {
func (app *App) registerUpgrade0_3_1(_ upgradetypes.Plan) {
const planName = "v0.3.1"

app.UpgradeKeeper.SetUpgradeHandler(planName,
func(ctx context.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
sdkCtx.Logger().Info("Upgrade handler execution", "name", planName)
return app.mm.RunMigrations(ctx, app.configurator, fromVM)
},
)
}

func (app *App) registerUpgrade0_3_2(_ upgradetypes.Plan) {
const planName = "v0.3.2"

app.UpgradeKeeper.SetUpgradeHandler(planName,
func(ctx context.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)

// migrate old proposals
govMigrator := govkeeper.NewMigrator(&app.GovKeeper, app.GetSubspace(govtypes.ModuleName))
err := govMigrator.Migrate2to3(sdkCtx)
err := migrateProposals(sdkCtx, app.keys[govtypes.StoreKey], app.appCodec)
if err != nil {
panic("failed to migrate governance module")
sdkCtx.Logger().Error("failed to migrate governance proposals", "err", err)
}

sdkCtx.Logger().Info("Upgrade handler execution", "name", planName)
Expand Down Expand Up @@ -281,3 +295,63 @@ func (app *App) storeUpgrade(planName string, ui upgradetypes.Plan, stores store
upgradetypes.UpgradeStoreLoader(ui.Height, &stores))
}
}

// migrateProposals migrates all legacy MsgUpgateGovParam proposals into non legacy param update versions.
func migrateProposals(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) error {
rbajollari marked this conversation as resolved.
Show resolved Hide resolved
store := ctx.KVStore(storeKey)
propStore := prefix.NewStore(store, v1migrations.ProposalsKeyPrefix)

iter := propStore.Iterator(nil, nil)
defer iter.Close()

for ; iter.Valid(); iter.Next() {
var prop govv1.Proposal
err := cdc.Unmarshal(iter.Value(), &prop)
// if error unmarshaling prop, convert to non legacy prop
if err != nil {
newProp, err := convertProposal(prop, cdc)
if err != nil {
return err
}
bz, err := cdc.Marshal(&newProp)
if err != nil {
return err
}
// Set new value on store.
propStore.Set(iter.Key(), bz)
}
}

return nil
}

func convertProposal(prop govv1.Proposal, cdc codec.BinaryCodec) (govv1.Proposal, error) {
msgs := prop.Messages

for _, msg := range msgs {
var oldUpdateParamMsg oracletypes.MsgLegacyGovUpdateParams
err := cdc.Unmarshal(msg.GetValue(), &oldUpdateParamMsg)
if err != nil {
return govv1.Proposal{}, err
}

newUpdateParamMsg := oracletypes.MsgGovUpdateParams{
Authority: oldUpdateParamMsg.Authority,
Title: oldUpdateParamMsg.Title,
Description: oldUpdateParamMsg.Description,
Plan: oracletypes.ParamUpdatePlan{
Keys: oldUpdateParamMsg.Keys,
Height: 0, // placeholder value for height
Changes: oldUpdateParamMsg.Changes,
},
}

msg.Value, err = newUpdateParamMsg.Marshal()
if err != nil {
return govv1.Proposal{}, err
}
}

prop.Messages = msgs
return prop, nil
}
Loading