-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: v0.3.2 upgrade handler (#481)
* chore: v0.3.2 upgrade handler * convert proposals * lint * migrate proposal test (cherry picked from commit faac055) # Conflicts: # app/upgrades.go
- Loading branch information
1 parent
787e16e
commit 5b46508
Showing
3 changed files
with
150 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / test-unit-cover
|
||
storetypes "cosmossdk.io/store/types" | ||
Check failure on line 5 in x/oracle/keeper/migrate_proposal.go GitHub Actions / test-unit-cover
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |