Skip to content

Commit

Permalink
chore: v0.3.2 upgrade handler (#481)
Browse files Browse the repository at this point in the history
* chore: v0.3.2 upgrade handler

* convert proposals

* lint

* migrate proposal test

(cherry picked from commit faac055)

# Conflicts:
#	app/upgrades.go
  • Loading branch information
rbajollari authored and mergify[bot] committed Jul 26, 2024
1 parent 787e16e commit 5b46508
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 2 deletions.
28 changes: 26 additions & 2 deletions app/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
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"
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 @@ -40,6 +39,11 @@ func (app App) RegisterUpgradeHandlers() {
app.registerUpgrade0_3_1Rc1(upgradeInfo)
app.registerUpgrade0_3_1Rc2(upgradeInfo)
app.registerUpgrade0_3_1(upgradeInfo)
<<<<<<< HEAD

Check failure on line 42 in app/upgrades.go

View workflow job for this annotation

GitHub Actions / lint

expected statement, found '<<' (typecheck)
=======
app.registerUpgrade0_3_2(upgradeInfo)
app.registerUpgrade0_4_0(upgradeInfo)
>>>>>>> faac055 (chore: v0.3.2 upgrade handler (#481))

Check failure on line 46 in app/upgrades.go

View workflow job for this annotation

GitHub Actions / lint

illegal character U+0023 '#' (typecheck)
}

// performs upgrade from v0.1.3 to v0.1.4
Expand Down Expand Up @@ -189,12 +193,32 @@ func (app *App) registerUpgrade0_3_1(_ upgradetypes.Plan) {
const planName = "v0.3.1"

app.UpgradeKeeper.SetUpgradeHandler(planName,
<<<<<<< HEAD

Check failure on line 196 in app/upgrades.go

View workflow job for this annotation

GitHub Actions / lint

expected operand, found '<<' (typecheck)
func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
// migrate old proposals
govMigrator := govkeeper.NewMigrator(&app.GovKeeper, app.GetSubspace(govtypes.ModuleName))
err := govMigrator.Migrate2to3(ctx)
=======
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)

Check failure on line 205 in app/upgrades.go

View workflow job for this annotation

GitHub Actions / lint

missing ',' in argument list (typecheck)
},

Check failure on line 206 in app/upgrades.go

View workflow job for this annotation

GitHub Actions / lint

expected operand, found '}' (typecheck)
)
}

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

Check failure on line 211 in app/upgrades.go

View workflow job for this annotation

GitHub Actions / lint

missing ',' in argument list (typecheck)

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

// migrate old proposals
err := oraclekeeper.MigrateProposals(sdkCtx, app.keys[govtypes.StoreKey], app.appCodec)

Check warning

Code scanning / CodeQL

Useless assignment to local variable Warning

This definition of err is never used.
>>>>>>> faac055 (chore: v0.3.2 upgrade handler (#481))

Check failure on line 219 in app/upgrades.go

View workflow job for this annotation

GitHub Actions / lint

expected statement, found '>>' (typecheck)
if err != nil {
panic("failed to migrate governance module")
sdkCtx.Logger().Error("failed to migrate governance proposals", "err", err)
}

ctx.Logger().Info("Upgrade handler execution", "name", planName)
Expand Down
72 changes: 72 additions & 0 deletions x/oracle/keeper/migrate_proposal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package keeper

import (
"cosmossdk.io/store/prefix"

Check failure on line 4 in x/oracle/keeper/migrate_proposal.go

View workflow job for this annotation

GitHub Actions / test-unit-cover

cannot find module providing package cosmossdk.io/store/prefix: import lookup disabled by -mod=readonly

Check failure on line 4 in x/oracle/keeper/migrate_proposal.go

View workflow job for this annotation

GitHub Actions / test-app-non-determinism

cannot find module providing package cosmossdk.io/store/prefix: import lookup disabled by -mod=readonly
storetypes "cosmossdk.io/store/types"

Check failure on line 5 in x/oracle/keeper/migrate_proposal.go

View workflow job for this annotation

GitHub Actions / test-unit-cover

cannot find module providing package cosmossdk.io/store/types: import lookup disabled by -mod=readonly

Check failure on line 5 in x/oracle/keeper/migrate_proposal.go

View workflow job for this annotation

GitHub Actions / test-app-non-determinism

cannot find module providing package cosmossdk.io/store/types: import lookup disabled by -mod=readonly
sdk "github.com/cosmos/cosmos-sdk/types"
v1migrations "github.com/cosmos/cosmos-sdk/x/gov/migrations/v1"

"github.com/cosmos/cosmos-sdk/codec"
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
oracletypes "github.com/ojo-network/ojo/x/oracle/types"
)

// MigrateProposals migrates all legacy MsgUpgateGovParam proposals into non legacy param update versions.
func MigrateProposals(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) error {
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
}
52 changes: 52 additions & 0 deletions x/oracle/keeper/migrate_proposal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package keeper_test

import (
types1 "github.com/cosmos/cosmos-sdk/codec/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"

"github.com/ojo-network/ojo/x/oracle/keeper"
"github.com/ojo-network/ojo/x/oracle/types"
)

func (s *IntegrationTestSuite) TestMigrateProposal() {
ctx := s.ctx
cdc := s.app.AppCodec()
storeKey := s.app.GetKey(govtypes.StoreKey)

// create legacy prop and set it in store
legacyMsg := types.MsgLegacyGovUpdateParams{
Authority: "ojo10d07y265gmmuvt4z0w9aw880jnsr700jcz4krc",
Title: "title",
Description: "desc",
Keys: []string{
"VotePeriod",
},
Changes: types.Params{
VotePeriod: 5,
},
}
bz, err := cdc.Marshal(&legacyMsg)
s.Require().NoError(err)
prop := govv1.Proposal{
Id: 1,
Messages: []*types1.Any{
{
TypeUrl: "/ojo.oracle.v1.MsgGovUpdateParams",
Value: bz,
XXX_unrecognized: []byte{},
},
},
Status: govv1.ProposalStatus_PROPOSAL_STATUS_PASSED,
}
s.app.GovKeeper.SetProposal(ctx, prop)

// try to retreive proposal and fail
_, err = s.app.GovKeeper.Proposals.Get(ctx, prop.Id)
s.Require().Error(err)

// succesfully retreive proposal after migration
err = keeper.MigrateProposals(ctx, storeKey, cdc)
_, err = s.app.GovKeeper.Proposals.Get(ctx, prop.Id)
s.Require().NoError(err)
}

0 comments on commit 5b46508

Please sign in to comment.