diff --git a/UPGRADING.md b/UPGRADING.md index 0d04b4906ba2..d0fc77178821 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -160,7 +160,7 @@ This allows you to remove the replace directive `replace github.com/gogo/protobu Please use the `ghcr.io/cosmos/proto-builder` image (version >= `0.11.5`) for generating protobuf files. -See which buf commit for `cosmos/cosmos-sdk` to pin in your `buf.yaml` file [here](./proto/README.md) +See which buf commit for `cosmos/cosmos-sdk` to pin in your `buf.yaml` file [here](./proto/README.md). #### `{accepts,implements}_interface` proto annotations @@ -231,9 +231,7 @@ modified to set the new parameter to the desired value. ##### New Proposal.Proposer field -The `Proposal` proto has been updated with proposer field. For proposal state migraton developers can call `v4.AddProposerAddressToProposal` in their upgrade handler to update all existing proposal and make them compatible and this migration is optional. - -> This migration is optional, if chain wants to cancel previous proposals which are active (deposit or voting period) they can do this proposals state migration. +The `Proposal` proto has been updated with proposer field. For proposal state migraton developers can call `v4.AddProposerAddressToProposal` in their upgrade handler to update all existing proposal and make them compatible and **this migration is optional**. ```go import ( diff --git a/x/gov/migrations/v4/store.go b/x/gov/migrations/v4/store.go index 9bd1a9b4ee8e..4f3b2becaae5 100644 --- a/x/gov/migrations/v4/store.go +++ b/x/gov/migrations/v4/store.go @@ -1,6 +1,9 @@ package v4 import ( + "fmt" + "sort" + "cosmossdk.io/store/prefix" storetypes "cosmossdk.io/store/types" @@ -8,6 +11,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/gov/exported" v1 "github.com/cosmos/cosmos-sdk/x/gov/migrations/v1" + "github.com/cosmos/cosmos-sdk/x/gov/types" govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" ) @@ -78,3 +82,50 @@ func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, legacySubspace return migrateParams(ctx, storeKey, legacySubspace, cdc) } + +// AddProposerAddressToProposal will add proposer to proposal and set to the store. This function is optional. +func AddProposerAddressToProposal(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec, proposals map[uint64]string) error { + proposalIDs := make([]uint64, 0, len(proposals)) + + for proposalID := range proposals { + proposalIDs = append(proposalIDs, proposalID) + } + + // sort the proposalIDs + sort.Slice(proposalIDs, func(i, j int) bool { return proposalIDs[i] < proposalIDs[j] }) + + store := ctx.KVStore(storeKey) + + for _, proposalID := range proposalIDs { + if len(proposals[proposalID]) == 0 { + return fmt.Errorf("found missing proposer for proposal ID: %d", proposalID) + } + + if _, err := sdk.AccAddressFromBech32(proposals[proposalID]); err != nil { + return fmt.Errorf("invalid proposer address : %s", proposals[proposalID]) + } + + bz := store.Get(types.ProposalKey(proposalID)) + var proposal govv1.Proposal + if err := cdc.Unmarshal(bz, &proposal); err != nil { + panic(err) + } + + // Check if proposal is active + if proposal.Status != govv1.ProposalStatus_PROPOSAL_STATUS_VOTING_PERIOD && + proposal.Status != govv1.ProposalStatus_PROPOSAL_STATUS_DEPOSIT_PERIOD { + return fmt.Errorf("invalid proposal : %s, proposal not active", proposals[proposalID]) + } + + proposal.Proposer = proposals[proposalID] + + // set the new proposal with proposer + bz, err := cdc.Marshal(&proposal) + if err != nil { + panic(err) + } + store.Set(types.ProposalKey(proposal.Id), bz) + } + + return nil +} diff --git a/x/gov/migrations/v5/store.go b/x/gov/migrations/v5/store.go deleted file mode 100644 index d0e17d0c604c..000000000000 --- a/x/gov/migrations/v5/store.go +++ /dev/null @@ -1,62 +0,0 @@ -package v5 - -import ( - "fmt" - "sort" - - storetypes "cosmossdk.io/store/types" - - "github.com/cosmos/cosmos-sdk/codec" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/gov/types" - govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" -) - -// AddProposerAddressToProposal will add proposer to proposal -// and set to the store. This function is optional, and only needed -// if you wish that migrated proposals be cancellable. -func AddProposerAddressToProposal(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec, proposals map[uint64]string) error { - proposalIDs := make([]uint64, 0, len(proposals)) - - for proposalID := range proposals { - proposalIDs = append(proposalIDs, proposalID) - } - - // sort the proposalIDs - sort.Slice(proposalIDs, func(i, j int) bool { return proposalIDs[i] < proposalIDs[j] }) - - store := ctx.KVStore(storeKey) - - for _, proposalID := range proposalIDs { - if len(proposals[proposalID]) == 0 { - return fmt.Errorf("found missing proposer for proposal ID: %d", proposalID) - } - - if _, err := sdk.AccAddressFromBech32(proposals[proposalID]); err != nil { - return fmt.Errorf("invalid proposer address : %s", proposals[proposalID]) - } - - bz := store.Get(types.ProposalKey(proposalID)) - var proposal govv1.Proposal - if err := cdc.Unmarshal(bz, &proposal); err != nil { - panic(err) - } - - // Check if proposal is active - if proposal.Status != govv1.ProposalStatus_PROPOSAL_STATUS_VOTING_PERIOD && - proposal.Status != govv1.ProposalStatus_PROPOSAL_STATUS_DEPOSIT_PERIOD { - return fmt.Errorf("invalid proposal : %s, proposal not active", proposals[proposalID]) - } - - proposal.Proposer = proposals[proposalID] - - // set the new proposal with proposer - bz, err := cdc.Marshal(&proposal) - if err != nil { - panic(err) - } - store.Set(types.ProposalKey(proposal.Id), bz) - } - - return nil -}